By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

RadioButtonList created in the page load event

P: n/a
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?

Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"William LaMartin" <la******@tampabay.rr.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?

Nov 18 '05 #2

P: n/a
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
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:e1*************@tk2msftngp13.phx.gbl...
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 in Page_PreRend phase.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"William LaMartin" <la******@tampabay.rr.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?


Nov 18 '05 #3

P: n/a
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.
"William LaMartin" <la******@tampabay.rr.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.