c# - 在 C# 中使用复选框启用和禁用 TextBox

标签 c# javascript asp.net .net .net-4.0

我想启用禁用我的文本框,因为用户可以在文本框中输入调用准备时间,也可以选择无限时间的复选框。

我的aspx代码

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
    <tr class="rowPadding">
        <td class="labelStyle" width="240px">
            <asp:Label runat="server" ID="lblPrepTime" Width="230px" meta:resourcekey="lblPrepTimeResource1"> Call preparation time (seconds): </asp:Label>
        </td>
        <td class="textBoxStyle" width="100px">
            <asp:TextBox runat="server" ID="txtPrepTime" Width="80px" meta:resourcekey="txtPrepTimeResource1"></asp:TextBox>
        </td>
        <td class="labelPaddingRight" width="300px">
            <asp:CheckBox runat="server" ID="cbUnlimited" Text="unlimited" meta:resourcekey="cbUnlimitedResource1" />
        </td>
    </tr>
</table>

我的代码是

protected void Page_Load(object sender, EventArgs e)
{
   try
   {
      if (!IsPostBack)
      {
          BindAllCallSettings();
      }
   }
   catch (Exception ex)
   {
         Logger.WriteException(ex);
   }
}

最佳答案

在您的客户端代码中添加此方法..

<script type="text/javascript">
    function EnableDisableCheckBox() {
        if (document.getElementById('<%=cbUnlimited.ClientID%>').checked) {
            document.getElementById('<%=txtPrepTime.ClientID%>').disabled = true;
        }
        else {
            document.getElementById('<%=txtPrepTime.ClientID%>').disabled = false;
        }
    }
</script>

然后将您的 onclick 事件替换为下面给出的,

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
    <tr class="rowPadding">
        <td class="labelStyle" width="240px">
            <asp:Label runat="server" ID="lblPrepTime" Width="230px" meta:resourcekey="lblPrepTimeResource1"> Call preparation time (seconds): </asp:Label>
        </td>
        <td class="textBoxStyle" width="100px">
            <asp:TextBox runat="server" ID="txtPrepTime" Width="80px" meta:resourcekey="txtPrepTimeResource1"></asp:TextBox>
        </td>
        <td class="labelPaddingRight" width="300px" onclick="EnableDisableCheckBox()">
            <asp:CheckBox runat="server" ID="cbUnlimited" Text="unlimited" meta:resourcekey="cbUnlimitedResource1" />
        </td>
    </tr>
</table>

在你后面的代码中使用这个,现在每当页面刷新并且你绑定(bind)你的数据时,它也会在页面加载时保存它的状态......

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            BindAllCallSettings();
        }
        this.txtPrepTime.Enabled = !(this.cbUnlimited.Checked);
    }
    catch (Exception ex)
    {
        Logger.WriteException(ex);
    }
}

关于c# - 在 C# 中使用复选框启用和禁用 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455996/

相关文章:

javascript - 如何使用表单中的值在javascript中增加值

javascript - 不可变的 : find if map contains key value

c# - 附加到 asp.net masterpage 中的默认标题

c# - WireMock.Net 如何响应有时错误和其他 OK

c# - Azure Active Directory 身份验证损坏 - 没有已知的根本原因

c# - 使用 SQL LIKE 运算符的 LINQ to Entities

c# - 从 derived 而不是 base 调用的虚拟方法

Javascript:动态创建菜单时菜单脚本没有反应

asp.net - .gitignore 用于 asp.net razor 项目

asp.net - 没有应用迁移。数据库已经是最新的