RadioButtonList created in the page load event

W

William LaMartin

If I create a RadioButtonList with, say, two items, then after the page
loads and I select one of the items and then click on a button whose click
event contains some code to display the RadioButtonlList's selected value,
there is nothing.

On a postback, with the dynamically created button list visible on the page
and the first item selected, if I try some code like "If
Me.RadioButtonList1.Items(0).Selected = True Then.......", then I get an
index out of range error. It is like the RadiobuttonList does not really
exits.

For RadioButtonLists that are simply dropped on a page with their values set
in the properties window in Visual Studio, there is no problem.

So, how does one determine whether a dynamically created RadioButtonList
item is selected and if so what its selected value is?
 
T

Teemu Keiski

Hi,

if you create the RadioButtonList at Page_load, it's state/data is available
after postback events which happen after Page_Load have occurred.

i.e page lifecycle on every request is:

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load (postback)
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

As you can see, so if on initial request you create the RadioButtonList in
Page_load, it hasn't yet loaded its state at Page_Load phase on postback. As
a solution, you could move the creation of control into Page_Init, when
state is available at Page_Load, or move checking the selection to PreRender
phase.
 
W

William LaMartin

Thanks for the suggestions. I put the code that creates the RadioButtonList
in Page_Init with no change. I then put the check for selected value in
Page_PreRender, also with no change. The code used is below.

I will study your sequence of events and see if that can somehow lead to a
solution to the problem. I have run into similar situations when I wanted
to set a Session variable and then use it on a button click, but the session
variable was a click behind when I needed it.

Code that creates the RadioButtonList:

Dim filepath As String
Dim filename As String
Dim RadioButtonList1 As New RadioButtonList
RadioButtonList1.ID = "AcrobatRBL"
RadioButtonList1.Visible = True
RadioButtonList1.EnableViewState = True
RadioButtonList1.AutoPostBack = True
Me.Panel1.Visible = True
Me.Panel1.Controls.Clear()
filepath = Server.MapPath(".") & "\acrobat_files\"
Dim CurrentDirectory As New System.IO.DirectoryInfo(filepath)
Dim FileArray() As System.IO.FileInfo
FileArray = CurrentDirectory.GetFiles()
Dim i As Integer
For i = FileArray.GetLowerBound(0) To FileArray.GetUpperBound(0)
Dim mylistItem As New ListItem
mylistItem.Text = FileArray(i).Name
mylistItem.Value = FileArray(i).Name
RadioButtonList1.Items.Add(mylistItem)
Next i
Me.Panel1.Controls.Add(RadioButtonList1)

Code to check the selected value:

Me.Label2.Text = Me.RadioButtonList2.SelectedValue
 
W

William LaMartin

Problem solved. It really had nothing to do with the RadioButtonList being
created on the fly. The problem was caused by there being to declarations
of the RadioButtonList--one at the page level and one at the page_load event
level. When I realized this and removed the one at the page_load event
level, there ceased to be a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top