Avatar of MMFQB9
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>Yes</asp:ListItem>
             <asp:ListItem Selected="True">No</asp:ListItem>
</asp:RadioButtonList>

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!!

Thanks.


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" + ",";
 
         }
    else
        {div.style.display = 'none';
         hidField.value += divID + ":none" + ",";
 
        }
return true;
}

Open in new window

JavaScriptASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
MMFQB9

8/22/2022 - Mon
jmwheeler

Have you tried using onchange instead of onclick?
ASKER
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.
ASKER CERTIFIED SOLUTION
jmwheeler

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
MMFQB9

Genius!!!!!!

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

Thank you for helping me!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes