Uncheck Radio Button on Double Click

Recently I got one requirement that i have to uncheck radio button list when a user double click it (Try to uncheck). I did this using JAVA Script.

Below is the code.

Designer:

<head runat="server">
    <title> :Radio Button List Demo: </title>
<script language="javascript" type="text/javascript">
    //Global variable to store selectedvalue
    var lastchecked = "";
 
    function rblSelectedValue() {
        //Get the radiobuttonlist Value
        var radio = document.getElementsByName('<%= RadioButtonList1.ClientID %>');
 
        //local variable to store selected value
        var selectedvalue;
 
        //loop through the items in radiobuttonlist
        for (var j = 0; j < radio.length; j++) {
            if (radio[j].checked) {
                if (lastchecked == radio[j].value) {
                    radio[j].checked = false;
                    lastchecked = "";
                }
                else {
                    lastchecked = radio[j].value;
                }               
                break;
            }
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label2" runat="server" Text="How many states are in India?"></asp:Label>
        <br />
        <asp:radiobuttonlist id="RadioButtonList1" onclick="rblSelectedValue()" runat="server">
           <asp:listitem value="26">Twenty Six</asp:listitem>
           <asp:listitem value="28">Twenty Eight</asp:listitem>
           <asp:listitem value="20">Twenty</asp:listitem>
           <asp:listitem value="30">Thirty</asp:listitem>
       </asp:radiobuttonlist>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>

Code:

protected void Button1_Click(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedItem != null)
        {
            if (RadioButtonList1.SelectedItem.Value=="28")
            {
                Label1.ForeColor = System.Drawing.ColorTranslator.FromHtml("Green");
                Label1.Text = "You have selected :" + RadioButtonList1.SelectedItem.Text;
            }
            else
            {
                Label1.ForeColor = System.Drawing.ColorTranslator.FromHtml("Red");
                Label1.Text = "You have selected : " + RadioButtonList1.SelectedItem.Text;
            }
             
        }
        else
        {
            Label1.ForeColor = System.Drawing.ColorTranslator.FromHtml("Red");
            Label1.Text = "None of the option selected";
        }
    }

Cheers!!!


No Comments

Add a Comment