Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I enable a radiobuttonlist only when a checkbox is checked?

I've tried with this code:
C#
if(cbCheck.Checked == true)
{
rblList.Enabled = true;
}
else
{
rblList.Enabled = false;
}

But, enabled state is not changing as checkbox cheked.
Posted
Updated 19-Dec-11 23:24pm
v2
Comments
Nigam Patel 20-Dec-11 23:00pm    
Can you please add your aspx page code?

try this:

C#
if(cbCheck.Checked == true)
{

            for (int i = 0; i < rblList.Items.Count; i++)
            {
                rblList.Items[i].Enabled = true;
            }
}
else
{
for (int i = 0; i < rblList.Items.Count; i++)
            {
                rblList.Items[i].Enabled = false;
            }
}


hope it helps :)
 
Share this answer
 
v2
Comments
Zukiari 20-Dec-11 6:41am    
I've checked with this also, but same result!!
Thank you.
Zukiari 20-Dec-11 6:46am    
When I've tried to that code an error occuered:'System.Web.UI.WebControls.ListItem' does not contain a definition for 'Enabled'
thank you
MAKE SURE IN ASPX page :
AutoPostBack="True" for your checkbox

Eg.
ASP.NET
<asp:checkbox id="cbCheck" runat="server" xmlns:asp="#unknown">
           oncheckedchanged="cbCheck_CheckedChanged" AutoPostBack="True" ></asp:checkbox>

C#
protected void cbCheck_CheckedChanged(object sender, EventArgs e)
    {
        if (cbCheck.Checked == true)
        {
            rblList.Enabled = true;
        }
        else
        {
            rblList.Enabled = false;
        }

    }
 
Share this answer
 
v3
Comments
Zukiari 20-Dec-11 6:41am    
I've tried that one also, no use for me....
Thankyou
anushripatil 20-Dec-11 6:55am    
can u just try this in a new web page ,
<asp:CheckBox ID="cbCheck" runat="server"
oncheckedchanged="cbCheck_CheckedChanged" AutoPostBack="True" />
<asp:RadioButtonList ID="rblList" runat="server" Enabled="false">
<asp:ListItem>Item 1
<asp:ListItem>Item 2
<asp:ListItem>Item 3
<asp:ListItem>Item 4
<asp:ListItem>Item 5
<asp:ListItem>Item 6



And in code behind
protected void cbCheck_CheckedChanged(object sender, EventArgs e)
{
if (cbCheck.Checked == true)
{
rblList.Enabled = true;
}
else
{
rblList.Enabled = false;
}

}
I've set the 'AutoPostback' property of checkbox as 'true' and got the correct output.
thanks for all.
 
Share this answer
 

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