G43beli
G43beli

Reputation: 3972

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 ID 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
Session.Remove("currentGender")

currentRadio 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?

Upvotes: 2

Views: 493

Answers (1)

Aytee
Aytee

Reputation: 567

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)

Upvotes: 2

Related Questions