Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an asp radiobuttonlist6 with 2 items namely, 'this', and 'that'. If I select 'that' , I want to show 'panel1'. If I select 'this' , I want to hide the 'panel1'. I tried with the following code to show atleast the 'panel1' on any of the button click. I could not succeed in it.
Can any one guide on how to meet my requirements using javascript function. I am poor in javascript. I submit the following with which I tried to some extent.

XML
<script type = "text/javascript">
    function Validate() {
         if (document.getElementById('<% = Radiobuttonlist6.ClientID %>').checked == 'true')
             document.getElementById('<% = Panel1.ClientID %>').style.display = "block";
        }
    }
</script>
<asp:RadioButtonList ID="Radiobuttonlist6" runat="server"
                    RepeatDirection="Vertical"  AutoPostBack="true" style=" margin-left:10px; margin-top:44px" >
                    <asp:ListItem>This</asp:ListItem>
                    <asp:ListItem>That</asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="Panel1" runat="server"  Width="33" style=" border-style:groove; height:30px; background-color:Red; margin-top:73px">
    </asp:Panel>


C#
protected void Page_Load(object sender, EventArgs e)
   {
       this.Panel1.Style.Add("display", "none");
       this.Radiobuttonlist6.Attributes.Add("Checked", "Validate()");
   }
Posted

You can handle it in javascript itselt.

Try

Markup Code:
<asp:RadioButtonList ID="Radiobuttonlist6" runat="server"
                    RepeatDirection="Vertical"  AutoPostBack="true" onclick="Validate('Radiobuttonlist6','Panel1')" style=" margin-left:10px; margin-top:44px" >
                  
<asp:ListItem Text="this" Value="this"></asp:ListItem>  
<asp:ListItem Text="this" Value="that"></asp:ListItem>   
</asp:RadioButtonList>


JS function :

C#
function Validate(rb, panel1)
{
   var rb = document.getElementById('rb');
   var panel = document.getElementById('Panel1');


   var rblist = rb.getElementsByTagName("input");
   for (var i = 0; i < rblist.length; i++)
   {
      if (rblist[i].checked)
      {
         if (rblist[i].value == "this")
         {
            panel.style.display = "block";

         }
         else if (options[i].value == "that")
         {
           panel.style.display = "none";

         }
      }
   }
}


Try and revert

Hope this helps...
 
Share this answer
 
v2
Comments
S.Rajendran from Coimbatore 30-Dec-13 7:27am    
Using your guidance, I apllied like this but doesnt work. At certain lines I changed panel to Panel1 and rb to Radiobuttonlist6.

<script type = "text/javascript">
function Validate(rb, panel1) {
var rb = document.getElementById('Radiobuttonlist6');
var panel1 = document.getElementById('Panel1');
var rblist = rb.getElementsByTagName("input");
for (var i = 0; i < rblist.length; i++) {
if (Radiobuttonlist6list[i].checked) {
if (Radiobuttonlist6list[i].value == "this") {
panel1.style.display = "block";
}
else if (Radiobuttonlist6list[i].value == "that") {
panel1.style.display = "none";
}
}
}
}
</script>
<asp:RadioButtonList ID="Radiobuttonlist6" runat="server"
RepeatDirection="Vertical" AutoPostBack="true" onclick="Validate('Radiobuttonlist6','Panel1')" style=" margin-left:10px; margin-top:44px" >
<asp:listitem text="this" value="this" xmlns:asp="#unknown">
<asp:listitem text="that" value="that" xmlns:asp="#unknown">

<asp:Panel ID="Panel1" runat="server" Width="33" style=" border-style:groove; height:30px; background-color:Red; margin-top:73px">


code behind:
protected void Page_Load(object sender, EventArgs e)
{
this.Panel1.Style.Add("display", "none");
this.Radiobuttonlist6.Attributes.Add("Checked", "Validate()");
JoCodes 30-Dec-13 8:33am    
remove this.Radiobuttonlist6.Attributes.Add("Checked", "Validate()"); from codebehind. as well as xmlns:asp="#unknown"
S.Rajendran from Coimbatore 30-Dec-13 10:21am    
This I used (not working):
<script type = "text/javascript">
function Validate(rb, panel1) {
var rb = document.getElementById('Radiobuttonlist6');
var panel1 = document.getElementById('Panel1');
var rblist = rb.getElementsByTagName("input");
for (var i = 0; i < rblist.length; i++) {
if (Radiobuttonlist6list[i].checked) {
if (Radiobuttonlist6list[i].value == "this") {
panel1.style.display = "block";
}
else if (Radiobuttonlist6list[i].value == "that") {
panel1.style.display = "none";
}
}
}
}
</script>
<asp:RadioButtonList ID="Radiobuttonlist6" runat="server"
RepeatDirection="Vertical" AutoPostBack="true" onclick="Validate('Radiobuttonlist6','Panel1')" style=" margin-left:10px; margin-top:44px" >
<asp:listitem text="this" value="this">
<asp:listitem text="that" value="that">

<asp:Panel ID="Panel1" runat="server" Width="33" style=" border-style:groove; height:30px; background-color:Red; margin-top:73px">


protected void Page_Load(object sender, EventArgs e)
{
this.Panel1.Style.Add("display", "none");
//this.Radiobuttonlist6.Attributes.Add("Checked", "Validate()");
}
JoCodes 30-Dec-13 10:34am    
Remove autopostback property for radiobutton
S.Rajendran from Coimbatore 30-Dec-13 21:50pm    
I removed 'Autopostback=true' and changed the following 2 lines like this:
var rb = document.getElementById("<%=Radiobuttonlist6.ClientID%>");
var panel1 = document.getElementById("<%=Panel1.ClientID%>");

This worked. Thanks.
JavaScript
<script type="text/javascript">
    function Validate(radioButtonList) {

            for (var i = 0; i < radioButtonList.rows.length; ++i) {

                if (radioButtonList.rows[i].cells[0].firstChild.checked) {

                    if (radioButtonList.rows[i].cells[0].firstChild.value == "This") {
                        document.getElementById('Panel1').style.display = "none";
                    }
                    else
                        document.getElementById('Panel1').style.display = "block";
                }

            }

        }

</script>
<asp:radiobuttonlist id="Radiobuttonlist6" onclick="Validate(this);" runat="server" xmlns:asp="#unknown">
                    RepeatDirection="Vertical" style=" margin-left:10px; margin-top:44px" >
                    <asp:listitem>This</asp:listitem>
                    <asp:listitem>That</asp:listitem>
</asp:radiobuttonlist>
<asp:panel id="Panel1" runat="server" width="33" style="border-style:groove; height:30px; background-color:Red; margin-top:73px" xmlns:asp="#unknown">
    </asp:panel>
 
Share this answer
 
v2
Comments
S.Rajendran from Coimbatore 30-Dec-13 10:24am    
This I used , not working:
<script type="text/javascript">
function Validate(Radiobuttonlist6) {

for (var i = 0; i < Radiobuttonlist6.rows.length; ++i) {

if (Radiobuttonlist6.rows[i].cells[0].firstChild.checked) {

if (Radiobuttonlist6.rows[i].cells[0].firstChild.value == "This") {
document.getElementById('Panel1').style.display = "none";
}
else
document.getElementById('Panel1').style.display = "block";
}

}

}

</script>
<asp:RadioButtonList ID="Radiobuttonlist6" onclick="Validate(this);" runat="server" xmlns:asp="#unknown"
RepeatDirection="Vertical" style=" margin-left:10px; margin-top:44px" >
<asp:listitem>This
<asp:listitem>That


<asp:panel id="Panel1" runat="server" width="33" style="border-style:groove; height:30px; background-color:Red; margin-top:73px" xmlns:asp="#unknown">


I changed certain things like to Radiobuttonlist6 etc
Its my bad while pasting the code on solution window i paste the code as it is and forget to format as c# code try following
C#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="test1" %>



<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
  
    <script type="text/javascript">
        function Validate(radioButtonList) {

            for (var i = 0; i < radioButtonList.rows.length; ++i) {

                if (radioButtonList.rows[i].cells[0].firstChild.checked) {

                    if (radioButtonList.rows[i].cells[0].firstChild.value == "This") {
                        document.getElementById('Panel1').style.display = "none";
                    }
                    else
                        document.getElementById('Panel1').style.display = "block";
                }

            }

        }

</script>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    
<asp:RadioButtonList id="Radiobuttonlist6" onclick="Validate(this);" runat="server"
                    RepeatDirection="Vertical" style=" margin-left:10px; margin-top:44px" >
                    <asp:Listitem>This</asp:Listitem>
                    <asp:Listitem>That</asp:Listitem>
</asp:RadioButtonList>
<asp:Panel id="Panel1" runat="server" width="33" style="border-style:groove; height:30px; background-color:Red; margin-top:73px">
    </asp:Panel>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900