We help IT Professionals succeed at work.
Get Started

ASP:RadioButtonList is losing it's value on postback when I add an onclick javascript attribute

Last Modified: 2012-05-06
Hello, I'm hoping this is easy.

I have an ASP:RadioButtonList placed on Panel1 on my page:

<asp:RadioButtonList ID="radReferencePreviousOrder" runat="server" RepeatDirection="Horizontal">
             <asp:ListItem Selected="True">No</asp:ListItem>

And a "Next" button:

<asp:Button ID="btnNext" runat="server" Text="Next">

When I click on "Next", it updates the "CurrentStep", and a different Panel1 is hidden, and Panel2 is displayed on my page.  When I click "Previous", the CurrentStep is decremented, and Panel2 is hidden, and Panel1 is displayed.  The radiobuttonlist selected maintained correctly because of viewstate.

My problem:  
When I try to add an "onClick" attribute to the radiobuttonlist.

Dim previousSAPOrderRBL As RadioButtonList
        previousSAPOrderRBL = CType(OrderHeaderFormView.FindControl("radReferencePreviousOrder"), RadioButtonList)
        previousSAPOrderRBL.Attributes.Add("onClick", String.Format("return showDivIfYes(""{0}"", ""divPreviousSAPOrder"", ""{1}"");", previousSAPOrderRBL.ClientID, hiddenField.ClientID))

The client-side script works great, but after I click "Next", and then "Previous" again, the value of my selected RadioButton is always reset to "No".  If I comment out the Attributes.Add line, then the onClick function does not get called, but the viewState works like a champ.

I tried removing the "Selected="True"", but then, after postback, the radiobuttonlist is just cleared altogether.

Here is my client side script.  What am I doing wrong?  Any help is appreciated!!


function showDivIfYes(rblID, divID, hiddenFieldID)
    //By appending _0 to the radioButtonList Name, you are actually
    //getting the name of the GROUP that the buttons belong to.
    rblID += "_0";
    var rbl = document.getElementsByName(rblID);
    var div = document.getElementById(divID);
    var hidField = document.getElementById(hiddenFieldID);
    if (rbl[0].value = "Yes" && rbl[0].checked)
        {div.style.display = 'block';
         hidField.value += divID + ":block" + ",";
        {div.style.display = 'none';
         hidField.value += divID + ":none" + ",";
return true;

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE