G43beli G43beli - 10 months ago 48
Vb.net Question

Find Radiobutton Control from ID String in vb.net

I have 3 radiobuttons in my aspx:

<asp:RadioButton ID="rdoMasculin"
runat="server" AutoPostBack="true"
Checked="true" GroupName="gender"
TextAlign="Right" />

<asp:RadioButton ID="rdoFeminin"
runat="server" AutoPostBack="true"
GroupName="gender" />

<asp:RadioButton ID="rdoAnonymous"
runat="server" AutoPostBack="true"
GroupName="gender" />

When sending the form I'm storing the
of the checked radiobutton in a Session variable:

for example:
Session("currentGender") = rdoZukuTermine.ID

When I'm getting back on the page in a later stage I'm using the following code in codebehind:

Dim currentRadio = CType(FindControl(Session("currentGender")), RadioButton)
currentRadio.Checked = True

ends up as a null reference. But when I'm checking the Session it contains the right ID as a string.
Can someone help me?

Answer Source

Is the RadioButton control directly in your .aspx or does it have any parent controls? if yes call Findcontrol() on it direct parent like this:

Dim currentRadio = CType(ParentControl.FindControl(Session("currentGender")), RadioButton)