473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

RadioButtonList created in the page load event

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
3 8952
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DotNetGruven | last post by:
Anyone have any pointers on how to set the Value and Selected attributes in a ListItem in a RadioButtonList that is in a DataGrid? Here's what I have ------DataGrid------ -- BoundColumn 0 --...
6
by: DotNetGruven | last post by:
I have a webform that has a DataGrid on it with a RadioButtonList in each row. It is a simple On & Off. When the User Clicks on either of the RadioButtons, I need to postback to the server and...
3
by: Mark Broadbent | last post by:
try as I might (and using different properties) this control always seems to be returning the first element of the control as the "selected" item even though I am selecting the second element. All...
5
by: Mike Salter | last post by:
I created a page that reads a DB for questions and possible answers (usuallyYes/No). I create a panel for each group of questions, and add a panel for each question to the Group panel. To the...
0
by: Quinn | last post by:
Hi, I have the same problem as this topic posted ...
4
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load (page_load) i create dynamically an html table which contains a checkbox and a radiobuttonlist in each tablerow . The...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hallo, I have a radiobuttonlist control that is added on a custom Web User Control. This control has a property that exposes the SelectedIndex property of the embedded radiobuttonlist. When...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
13
by: tommymo | last post by:
Hi everyone I'm new to this site and the world of ASP.Net C# programming. I have been learning controls and integrating them with a SQL database. So far I have been able to move along and understand...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.