fishmong3r
fishmong3r

Reputation: 1434

How to force enable/disable of controls in aspx?

I have a simple windows form app which I have to migrate to run on a webpage, so I'm trying with aspx(c#).

I have two radio buttons, but at time only one of them should be checked. I implemented the very same code from the original app, but it's not working:

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton1.Checked == true)
        {
            RadioButton2.Checked = false;
        }
    }
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton2.Checked == true)
        {
            RadioButton1.Checked = false;
        }
    }

So why these changes not being applied on the page?

Upvotes: 0

Views: 1526

Answers (3)

Koen
Koen

Reputation: 2571

You can set the GroupName of both radiobuttons the same, so the Code Behind you wrote is not needed anymore.

Also, check out the RadioButtonList

UPDATE

Your aspx-code should look something like this:

<asp:RadioButton id="RadioButtonEnabled" runat="server" Text="Enabled" GroupName="GroupTxtEnabled" AutoPostBack="True" OnCheckedChanged="RadioButtonEnabled_CheckedChanged"/>
<asp:RadioButton id="RadioButtonDisabled" runat="server" Text="Disabled"  GroupName="GroupTxtEnabled" AutoPostBack="True"/>

<asp:TextBox id="TextBox1" runat="server"/>

And in your code-behind:

protected void RadioButtonEnabled_CheckedChanged (object sender, EventArgs e)
{
    TextBox1 = RadioButtonEnabled.Checked
}

As you can see, I've used the OnCheckedChanged only at one radiobutton, since both radiobuttons are changed at the same time (property GroupName is equal).

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

you can use GroupName Property of RadioButton Control.

if you set the same GroupName for set of RadioButtons you don't need to write the Code Behid which you have written in the above Post/Question, as only one radio button can be selected from the group.

but if you want to invoke some action like Disabling TextBox on Particular Radio Button click event you can use following sample code.

Example: in this example i'm taking 3 Radio Buttons all set to Same GroupName, hence only one RadioButton canbe selected at a time.

when user selects/checks a RadioButton1 i'm Disabling the TextBox1.

Note : Please Make sure that your RadioButton AutoPostBack Property set to True otherwise events on RadioButton won't be fired.

Design Code:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RadioButton ID="RadioButton1" runat="server" Text="DisableTextBox" GroupName="Group1" AutoPostBack="True" OnCheckedChanged="RadioButton1_CheckedChanged"/>
    <asp:RadioButton ID="RadioButton2" runat="server"  GroupName="Group1" AutoPostBack="True"/>
    <asp:RadioButton ID="RadioButton3" runat="server"  GroupName="Group1" AutoPostBack="True" />

Code Behind:

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
                TextBox3.Enabled = false;
            else
                TextBox3.Enabled = true;
        }

Upvotes: 1

Jeswills
Jeswills

Reputation: 357

Put the checkboxes in a GroubBox or Panel

Upvotes: 0

Related Questions