Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何强制启用/禁用aspx中的控件?_C#_Asp.net_Visual Studio 2010_Webforms - Fatal编程技术网

C# 如何强制启用/禁用aspx中的控件?

C# 如何强制启用/禁用aspx中的控件?,c#,asp.net,visual-studio-2010,webforms,C#,Asp.net,Visual Studio 2010,Webforms,我有一个简单的windows窗体应用程序,我必须迁移它才能在网页上运行,所以我正在尝试使用aspx(c#) 我有两个单选按钮,但当时只有一个应该检查。我实现了与原始应用程序完全相同的代码,但它不起作用: protected void RadioButton1_CheckedChanged(object sender, EventArgs e) { if (RadioButton1.Checked == true) { RadioBu

我有一个简单的windows窗体应用程序,我必须迁移它才能在网页上运行,所以我正在尝试使用aspx(c#)

我有两个单选按钮,但当时只有一个应该检查。我实现了与原始应用程序完全相同的代码,但它不起作用:

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;
        }
    }
那么为什么不在页面上应用这些更改呢?

您可以将两个单选按钮的设置相同,这样您编写的代码就不再需要了

另外,请查看

更新

您的aspx代码应该如下所示:

<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"/>

如您所见,我只在一个单选按钮上使用了
OnCheckedChanged
,因为两个单选按钮都是同时更改的(属性
GroupName
相等)。

将复选框放在GroubBox或Panel

您可以使用
radiobutton
控件的
GroupName
属性

如果您为一组
单选按钮设置了相同的
GroupName
,则无需编写您在上述帖子/问题中编写的代码,因为只能从组中选择一个单选按钮

但如果您想在特定单选按钮单击事件上调用某些操作,如禁用
TextBox
,则可以使用以下示例代码

示例:在这个示例中,我将3个单选按钮都设置为相同的
GroupName
,因此一次只能选择一个
RadioButton

当用户选择/检查RadioButton1时,我禁用TextBox1

注意:请确保您的
RadioButton
自动回发
属性设置为
True
,否则将不会触发
RadioButton
上的事件

设计代码:

<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" />
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
                TextBox3.Enabled = false;
            else
                TextBox3.Enabled = true;
        }

是的,这是一个解决这个问题的方法,但对于未来,我必须知道如何将我在代码中所做的更改强制到任何控件上。例如,我想禁用radiobutton选择事件上的文本框。怎么做?你确定你的活动在你的ASPX页面中连接正确吗?你的确切意思是什么?用一个例子更新了我的答案。再次。。。是的,这是一个解决这个问题的方法,但对于未来,我必须知道如何将我在代码中所做的更改强制到任何控件上。例如,我想禁用radiobutton选择事件上的文本框。如何做到这一点?您可以在我的示例中看到,当您选择radiobutton1时,它将禁用Textbox1。
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
                TextBox3.Enabled = false;
            else
                TextBox3.Enabled = true;
        }