【问题标题】:How to Set Checked and Enabled Property On CheckBox?如何在 CheckBox 上设置 Checked 和 Enabled 属性?
【发布时间】:2019-11-06 20:04:42
【问题描述】:

我想做一个两者兼有的复选框

Checked = true

Enabled = false

我该怎么做?

我编写了这段代码,但它从复选框中删除了Checked

chkDecreaseAbsenceFromExtraWork.Enabled = !SecurityManager.HasAccess(Session, AccessCode.EditDecreaseAbsenceFromExtraWorkIsImpossible); 

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    如果你有一个 CheckBox 控件

    <asp:CheckBox ID="CheckBox1" runat="server" />
    

    您将其设置为选中和禁用

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            CheckBox1.Enabled = false;
            CheckBox1.Checked = true;
        }
    }
    

    Eugene Podskal 的正确之处在于未提交值。但是 ViewState 仍然会在 PostBack 之后将 CheckBox 设置为选中。

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "CheckBox is " + CheckBox1.Checked;
    }
    

    【讨论】:

      【解决方案2】:

      不幸的是,这似乎是 HTML 本身的设计特性 - values of disabled inputs will not be submitted?

      你可以试试

      1. 要么使用 hidden fields 并将其复选框的值与表单中的它同步 - 也就是说,如果您确实需要禁用输入。
      2. 或者你可以这样做checkbox readonly

      【讨论】:

        猜你喜欢
        • 2010-11-30
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        • 2017-01-10
        • 2021-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多