Link to home
Start Free TrialLog in
Avatar of Steven O'Neill
Steven O'NeillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Radiobuttonlist onclick event

I'm pretty new to .net development but I'm looking to add an onclick event to a radiobuttonlist that will allow me to show hidden divs within a form. So I'm look for:

o Yes  o No

If Yes is selected then display hidden div 1/hide hidden div 2 and if No is selected the display hidden div 2/hide hidden div 1. I'd also like the ability to clear any completed info within the hidden divs. Again if a user chooses Yes then all content that may have been completed in hidden div 2 should be cleared and subsequently, if they choose No (they may change their mind) then it should clear eveything within hidden div 1.

Hope this makes sense

Thanx for any advice.
Avatar of jeebukarthikeyan
jeebukarthikeyan
Flag of India image

hi,
pass the radion button id

function display(rad)
{
  var divid1    =    document.getElementById('div1');
  var divid2    =    document.getElementById('div2');

  if(rad=0)
  {
    divid1.style.visibility="visible";
    divid2.style.visibility="hidden";
  }
  else
  {
    divid2.style.visibility="visible";
    divid1.style.visibility="hidden";
  }
 
}

b u d d h a
ASKER CERTIFIED SOLUTION
Avatar of noulouk
noulouk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steven O'Neill

ASKER

noulouk

Thanx for this. Where on my page should I have your event? Should I add this at the tope of the page within the script to runat server? I've done that but there are a host of errors within the display screen (I'm using Visual Web Developer 2005). Can you perhaps give a complete page for me to see and disect?
Avatar of badalpatel
badalpatel

use this javascript for radiobutton change

 function radiodatechanged()
            {  
               
                if(document.getElementById("Radiobutton1").checked == true)
                {
                             document.getElementById("textbox1").value =""; 'likewise clear all text boxes  in div1                      
                             document.getElementById("div1").visibility = 'hidden';  
                             document.getElementById("div2").visibility = 'visible';  
                }
                 if(document.getElementById("Radiobutton2").checked == true)
                {
                             document.getElementById("textbox2").value =""; 'likewise clear all text boxes  in div1                      
                             document.getElementById("div2").visibility = 'hidden';  
                             document.getElementById("div1").visibility = 'visible';  
                }
}

and in aspx.vb page, in page load event u have to write this two line to invoke javascript function

Radiobutton1.Attributes.Add("onclick", "radiodatechanged()")
Radiobutton2.Attributes.Add("onclick", "radiodatechanged()")
The radiobutton is created using the Visual Web Developer tool and appears like this:

<asp:RadioButtonList ID="RadioButtonList3" runat="server" Width="116px" RepeatDirection="Horizontal">
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>

How do I go about adding the ids to the listitem? I can see how to add values, etc but not an id.

Sorry for sounding so dumb but it's a bit different to normal ASP.

Thanx
OK, you needed a js function.

RadioButtonList1.Items.FindByValue("Yes").Attributes.Add("onclick", "radiodatechanged()");
RadioButtonList1.Items.FindByValue("No").Attributes.Add("onclick", "radiodatechanged()");

Something like this.
Or RadioButtonList1.Items.FindByText("Yes").Attributes.Add("onclick", "radiodatechanged()");
When I look at the html of my code I see this:

       <table id="RadioButtonList3" border="0" style="width:116px;">
            <tr>
                  <td><input id="RadioButtonList3_0" type="radio" name="RadioButtonList3" value="Yes" /><label for="RadioButtonList3_0">Yes</label></td><td><input id="RadioButtonList3_1" type="radio" name="RadioButtonList3" value="No" /><label for="RadioButtonList3_1">No</label></td>
            </tr>
      </table>

So I'm really confused by this...does using this method automatically wrap everything in table tags?

I'm also receiving JS errors on my page as I assume it cannot find the RadioButtonList3 as the error states it's undefined (at the location I place the second last piece of code supplied by noulouk.
I have the following at the top of my page:

<script language=javascript>
 function radiodatechanged()
            {  
               
                if(document.getElementById("Radiobutton1").checked == true)
                {
//                             document.getElementById("textbox1").value =""; 'likewise clear all text boxes in div1                      
                             document.getElementById("div1").visibility = 'visible';  
                             document.getElementById("div2").visibility = 'hidden';  
                             document.getElementById("morepeople").visibility = 'visible';  
                }
                 if(document.getElementById("Radiobutton2").checked == true)
                {
//                             document.getElementById("textbox2").value =""; 'likewise clear all text boxes in div2                      
                             document.getElementById("div1").visibility = 'hidden';  
                             document.getElementById("div2").visibility = 'visible';  
                             document.getElementById("morepeople").visibility = 'visible';  
                }
}

    RadioButtonList3.Items.FindByValue("Yes").Attributes.Add("onclick", "radiodatechanged()");
    RadioButtonList3.Items.FindByValue("No").Attributes.Add("onclick", "radiodatechanged()");

</script>

Within the form I have:

      <div style="display: block; visibility: visible; padding-bottom: 10px;">
                  <strong>Are you in business?</strong>
        <asp:RadioButtonList ID="RadioButtonList3" runat="server" Width="116px" RepeatDirection="Horizontal">
            <asp:ListItem>Yes</asp:ListItem>
            <asp:ListItem>No</asp:ListItem>
        </asp:RadioButtonList>
    </div>

but the HTML displayed is:

       <table id="RadioButtonList3" border="0" style="width:116px;">
            <tr>
                  <td><input id="RadioButtonList3_0" type="radio" name="RadioButtonList3" value="Yes" /><label for="RadioButtonList3_0">Yes</label></td><td><input id="RadioButtonList3_1" type="radio" name="RadioButtonList3" value="No" /><label for="RadioButtonList3_1">No</label></td>
            </tr>
      </table>

Any ideas to resolve this are grwaty appreciated.
I didn't use the code exactly as laid out but I did use panls to deliver my solution eventually. Thanx guys.