Solved

Radiobuttonlist onclick event

Posted on 2006-11-14
11
1,775 Views
Last Modified: 2008-02-07
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.
0
Comment
Question by:Steven O'Neill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 17937182
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
0
 
LVL 9

Accepted Solution

by:
noulouk earned 500 total points
ID: 17937192
Hi  u101440,

Put the SelectedValue of your Yes RadioButton to "Yes".
Then add this event:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue == "Yes")
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
           // Label1.Text="What you want";
        }
        else
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }

No matter if there are some datas inside the panels  : you don't have to clear this text because the panel is not shown.

Hope this helps.
Put a label inside one of your panels  and write what you want when you show the panel.
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 17937244
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:badalpatel
ID: 17937266
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()")
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 17937305
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
0
 
LVL 9

Expert Comment

by:noulouk
ID: 17937364
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.
0
 
LVL 9

Expert Comment

by:noulouk
ID: 17937366
Or RadioButtonList1.Items.FindByText("Yes").Attributes.Add("onclick", "radiodatechanged()");
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 17937563
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.
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 17937820
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.
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 18128748
I didn't use the code exactly as laid out but I did use panls to deliver my solution eventually. Thanx guys.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question