简体   繁体   English

ASP.NET C#Web应用程序-复选框

[英]ASP.NET C# Web application - CheckBoxes

I have two checkboxes, what I want to do is, when I check one, the other should be disabled, I always do this in C# Windows application and it is my first try with ASP.NET is there a way to do that without using combocheckboxes? 我有两个复选框,我想做的是,当我选中一个复选框时,另一个复选框应该被禁用,我总是在C#Windows应用程序中执行此操作,这是我第一次尝试使用ASP.NET,有没有一种方法可以不使用它combocheckboxs? here is my method which does not work: 这是我的方法不起作用:

protected void checkplan0_CheckedChanged(object sender, EventArgs e)
{
    if (checkplan0.Checked == true)
    {
        checkplan1.Enabled = false;
    }
    if (checkplan0.Checked == false)
    {
        checkplan1.Enabled = true;

    }
}

Like others have said you'll need to have autopostback="true", also it might be worth considering using radio buttons, as only one radio button in a group can be checked at a time. 就像其他人所说的那样,您将需要具有autopostback =“ true”,这也值得考虑使用单选按钮,因为一次只能检查组中的一个单选按钮。

<asp:RadioButton id="radioplan0" Checked="True" GroupName="RadioPlan" runat="server" Autopostback="true" />
<asp:RadioButton id="radioplan1" Checked="False" GroupName="RadioPlan" runat="server" Autopostback="true" />

Then nothing needs to be added to the code behind to toggle the other options off. 然后,无需在后面的代码中添加任何内容即可关闭其他选项。

Your code seems to be correct, as you mentioned that you are from windows form background I assume this is what you are missing 您的代码似乎是正确的,因为您提到您来自Windows窗体背景,我认为这是您所缺少的

<asp:CheckBox 
      ID="checkplan0" 
      runat="server" 
      AutoPostBack="true" 
      OnCheckedChanged="checkplan0_CheckedChanged" />

Set AutoPostBack = "true" setting this true will mean on check of checkbox a postback will be sent to server and the code that you have written on Check Change will execute. 设置AutoPostBack = "true"设置为true将意味着在选中复选框后,将向服务器发送回发邮件,并且将执行您在Check Change上编写的代码。

Your code seems good. 您的代码似乎不错。 But as you are beginner. 但是,因为您是初学者。 You might be missing simple thing in source page. 您可能在源页面中缺少简单的东西。

=> Check whether Your AutoPostBack set to true for your checkbox. =>检查复选框的AutoPostBack是否设置为true If not add AutoPostBack ="true" 如果没有,请添加AutoPostBack =“ true”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM