我想启用禁用我的文本框,因为用户可以在文本框中输入调用准备时间,也可以选择无限时间的复选框。
我的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/