Thursday, March 29, 2012

List controls....

Hi everyone,

I have a page with bunch of list controls with postback enabled.. after the selection is made, postback event is fired but the selected values don't stay selected after the postback..

the control randomly selects a different value from the list.. why?

I have viewstate enabled for page/app/control..

PS: I am not loading/binding the control values on page load event.

here is my source...


<td valign="top" class="LightGray">
<asp:RadioButtonList id="lstmarried1" runat="server" CssClass="form" CellSpacing="1" RepeatColumns="2"CellPadding="1" AutoPostBack="True"
<asp:ListItem Value="2" Selected="True">Single</asp:ListItem>
<asp:ListItem Value="1">Engaged</asp:ListItem>
<asp:ListItem Value="1">Married</asp:ListItem>
<asp:ListItem Value="2">Divorced</asp:ListItem>
<asp:ListItem Value="2">Separated</asp:ListItem>
</asp:RadioButtonList></td>
</tr

CODE Behind..


Private Sub lstmarried1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstmarried1.SelectedIndexChanged

If lstmarried1.SelectedItem.Value = 1 Then
p1.Visible = True
p2.Visible = True
Else
p1.Visible = False
p2.Visible = False
End If
End Sub

Show us the code in the Page_Load function plz.
My guess is that you use the same values in the listitem object for multiple items. Ex : Single, Divorced and Seperated = 2

I suppose when the page is post back, you get one of these 3 results checked instead the one you selected first.

Try changing the values to unique values, I think that will fix the problem, of course you will need to change your validation "If lstmarried1.SelectedItem.Value = 1 Then" to something else.

A suggestion :


<asp:ListItem Value="3" Selected="True">Single</asp:ListItem
<asp:ListItem Value="1">Engaged</asp:ListItem
<asp:ListItem Value="2">Married</asp:ListItem
<asp:ListItem Value="4">Divorced</asp:ListItem
<asp:ListItem Value="5">Separated</asp:ListItem

And change the code behind for something like that


If lstmarried1.SelectedItem.Value > 2 Then

p1.Visible = True

p2.Visible = True

Else

p1.Visible = False

p2.Visible = False

End If

Or you can remove the value field and it will get the text value instead the number. You can then switch through the choices of "lstmarried1.SelectedItem.Value"
Hi hexenjag,

Thanks mate you were spot on..!

I just changed the value of the list items and it just worked fine. It funny how Asp.NET is just depending on the value param or the control

Cheers!

0 comments:

Post a Comment