使用C#中的复选框启用和禁用TextBox

时间:2014-01-30 12:01:23

标签: 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);
   }
}

2 个答案:

答案 0 :(得分:2)

使用javascript:

<asp:CheckBox runat="server" ID="cbUnlimited" Text="unlimited" meta:resourcekey="cbUnlimitedResource1" onchange="myfn()"/>

<script type="text/javascript">
    function myfn(){
    var val=document.getElementById("txtPrepTime");`
    if(this.val==checked)
    {
        val.style.visibility=true;//or false as you want
    } 
    else 
    {
        val.style.visibility=true;//or false as you want  
    }
}
</script>

答案 1 :(得分:1)

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

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

在您的代码中使用此功能,现在每当刷新页面并绑定数据时,它都会在页面加载时保存其状态......

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