Link to home
Create AccountLog in
Avatar of MMFQB9

asked on

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

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)
        { = 'block';
         hidField.value += divID + ":block" + ",";
        { = 'none';
         hidField.value += divID + ":none" + ",";
return true;

Open in new window

Avatar of jmwheeler

Have you tried using onchange instead of onclick?
Avatar of MMFQB9


onchange does not work for me.. I need it to be client-side.    

I tend to be wordy with my explanations, here it is short.....

When I use the attributes.add method to add an onClick event to an asp:radiobuttonlist, the radiobuttonlist selected value does not persist across postbacks.  It always reverts back to its default.  Without the onClick event, the radiobuttonlist works as expected.
Avatar of jmwheeler

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of MMFQB9



That's what I was missing.... Wow, something so small, yet it caused me such a big problem.

Thank you for helping me!