question about postback and viewstate

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi,

There are two radiobuttonlists created and fed dynamically. Both has
AutoPostBack=True.
When an option of a radiobuttonlist is checked, its ID and its selectedvalue
must appear on screen (with response.write). The property EnableViewState is
set to False for the page.

It works but i have two questions about this:

1) when i start by clicking e.g. option 2 of radiobuttonlist 1 (ID=rb1), i
see according the code below: rb1 2

My question is: how can the server, after loading the postback data, assign
it to radiobuttonlist 1? At the moment of the load postback data step in the
Page Life Cycle, the control radiobuttonlist 1 doesn't exist yet, because
it's created dynamically during the Page_Load event (which comes later) ?

2) Now, after having clicking on option 2 of radiobuttonlist 1, clicking
e.g. on option 1 of radiobuttonlist 2 (ID=rb2) shows on screen: rb1 2 rb2 1

My question is: why do i still see: rb1 2? I expect to see only rb2 1,
because the SelectedIndex of radiobuttonlist 1 hasn't changed. Obviously,
both radiobuttonlist 1 and radiobuttonlist 2 passed trhough procedure
"radiod", but why radiobuttonlist1 because its selectedindex remained the
same?

I also tried with EnableViewState=True, and then i see only rb2 1 ... Why?

Thanks
Ben

The code:
--------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim rb1, rb2 As RadioButtonList
Dim z1, z2 As ListItem

rb1 = New RadioButtonList
rb1.AutoPostBack = True
rb1.ID = "rb1"
z1 = New ListItem("option 1 of radio 1", "1")
z2 = New ListItem("option 2 of radio 1", "2")
rb1.Items.Add(z1)
rb1.Items.Add(z2)
form1.Controls.Add(rb1)

AddHandler rb1.SelectedIndexChanged, AddressOf radiod

rb2 = New RadioButtonList
rb2.AutoPostBack = True
rb2.ID = "rb2"
z1 = New ListItem("option 1 of radio 2", "1")
z2 = New ListItem("option 2 of radio 2", "2")
rb2.Items.Add(z1)
rb2.Items.Add(z2)
form1.Controls.Add(rb2)

AddHandler rb2.SelectedIndexChanged, AddressOf radiod

End Sub

Protected Sub radiod(ByVal sender As Object, ByVal e As System.EventArgs)
Dim rb As RadioButtonList = CType(sender, RadioButtonList)
Response.Write(rb.ID & " " & rb.SelectedValue & " ")
End Sub
 
Back
Top