RadioButtonList behavior

M

MattB

Hi, I'm working with a RadioButtonList in a Web Form and it's not behaving
as I was expecting. I have it bound to a table in a dataset and it displays
like I want it to. Here's that chunk of code:

With RadioButtonList1

..DataSource = dsModifiers.Tables(1)

..DataTextField = "descrip"

..DataBind()

End With

-------------------------

Now my plan was to grab the SelectIndex Property when the form is submitted
and use that index to grab multiple values from the corresponding row in the
table.

The problem I'm having, is in the click event I always get a value of -1 for
RadioButtonList1.SelectedIndex when something is selected.

Is this a variable access issue between the Page_Load and Click events (I
didn't think it would be) or something else? Any hints for getting that
SelectIndex to be correct? Thanks!


--

Matt


"Gravity. It's not just a good idea, it's the law!"
 
G

Guest

Hi Matt

In my example, I couldn't duplicate your problem. I created a form with radList and btnTest, and here's the code-behind (vb syntax)

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Loa

Dim dt As New DataTabl
If Not Page.IsPostBack The
' Build a datatabl
dt = New DataTabl
dt.Columns.Add("Desc"
Dim s() As String = {"Bill"
dt.Rows.Add(s
Dim t() As String = {"Fred"
dt.Rows.Add(t
Dim u() As String = {"Mary"
dt.Rows.Add(u
radList.DataSource = d
radList.DataTextField = "Desc
radList.DataBind(
End I

End Su

Private Sub Page_PreRender(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.PreRende

End Su

Private Sub btnTest_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnTest.Clic

End Su
End Clas

When I click on "Mary", I get radList.SelectedIndex = 2 whether I break at PageLoad, PagePrerender, or ButtonClick after the postback (note that button click event is processed after the load but before the prerender, which has important implications; in cases where you want the code behind the button to *affect* what is displayed, you'll want that display code in the prerender instead of the load, because the load routine will have already come and gone before your button code is processed; can't tell from your snippet whether this is part of your issue or not)

I am also presuming you have not disabled viewstate for the form or the radiolist, and am presuming that you have created your radiolist statically (i.e. dropped on the form) vs. dynamically where your click and your load might not be seeing the same instance of the radiolist

Not much, but hopefully this will prod an idea

Regards

Bil

----- MattB wrote: ----

Hi, I'm working with a RadioButtonList in a Web Form and it's not behavin
as I was expecting. I have it bound to a table in a dataset and it display
like I want it to. Here's that chunk of code

With RadioButtonList

..DataSource = dsModifiers.Tables(1

..DataTextField = "descrip

..DataBind(

End Wit

------------------------

Now my plan was to grab the SelectIndex Property when the form is submitte
and use that index to grab multiple values from the corresponding row in th
table

The problem I'm having, is in the click event I always get a value of -1 fo
RadioButtonList1.SelectedIndex when something is selected

Is this a variable access issue between the Page_Load and Click events (
didn't think it would be) or something else? Any hints for getting tha
SelectIndex to be correct? Thanks


--

Mat


"Gravity. It's not just a good idea, it's the law!
 
M

MattB

Thank you. I'm still not sure exactly what was wrong, but I think it was
something to do with postback. I cleaned up the code a bit being very
careful with what got executed when posting back and that seemed to fix it.

Matt
 
G

Guest

I saw your other post--looks like you got it figured out. Imho, understanding in true depth what's happening re state in ASP.NET, esp. if you've written a lot of Windows apps, is the hardest part about getting up to speed. There's a great article on dotnetjunkies.com by Peter van Ooijen, "DataGrid revisited...", that you might want to check out. It's a nice overview of what's happening when. Good luck

----- MattB wrote: ----

Thank you. I'm still not sure exactly what was wrong, but I think it wa
something to do with postback. I cleaned up the code a bit being ver
careful with what got executed when posting back and that seemed to fix it

Mat

Bill Borg wrote
 

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

Top