我们尝试使用 C# 将 checkBox1 链接到 txtCommentBox。我们正在努力使 txtCommentBox 保持禁用状态,直到选中 checkBox1。
我们已经完成了以下内容。
if (checkBox1.Enabled)
{
txtCommentBox.Enabled = true;
}
失败后,我们在 page_Load 方法中尝试执行以下操作。
txtCommentBox.Enabled = checkBox1.Enabled;
那也没用。我们已经用控件尝试了各种属性,但没有成功。 .aspx 代码在下方,C# 代码在下方。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Infomation.aspx.cs" Inherits="Infomation" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Information</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 605px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>
If you would like to leave your Questions, Comments, E-mail, Name or Phone
Number check off the box you would like to enter into the form.
</h4>
<table class="style1">
<tr>
<td class="style2">
<asp:Label ID="Label1" runat="server"><span class="accesskey">C</span>omment:</asp:Label>
<asp:TextBox ID="txtCommentBox" runat="server" AccessKey="C" Width="334px"
ontextchanged="txtCommentBox_TextChanged" Enabled="False"></asp:TextBox>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack = "true"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server"><span class="accesskey">E</span>mail:</asp:Label>
<asp:TextBox ID="txtEmailBox" runat="server" AccessKey="E" Width="334px"
ontextchanged="txtEmailBox_TextChanged" Enabled="False"></asp:TextBox>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack = "true"
oncheckedchanged="CheckBox2_CheckedChanged" Text="Enable/disable" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server"><span class="accesskey">N</span>ame:</asp:Label>
<asp:TextBox ID="txtNameBox" runat="server" AccessKey="N" Width="334px"
ontextchanged="txtNameBox_TextChanged" Enabled="False"></asp:TextBox>
<asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack = "true"
oncheckedchanged="CheckBox3_CheckedChanged" Text="Enable/disable" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server"><span class="accesskey">P</span>hone Number:</asp:Label>
<asp:TextBox ID="txtPhoneBox" runat="server" AccessKey="P" Width="334px"
ontextchanged="txtPhoneBox_TextChanged" Enabled="False"></asp:TextBox>
<asp:CheckBox ID="CheckBox4" runat="server" AutoPostBack = "true"
oncheckedchanged="CheckBox4_CheckedChanged" Text="Enable/disable" />
</td>
<td>
</td>
</tr>
</table>
<asp:ListBox ID="ListBox1" runat="server" Height="321px" Width="887px">
</asp:ListBox>
<br />
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Back to the Main Page" />
<br />
</div>
</form>
</body>
</html>
我已经将我们的 C# 代码放在下面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Infomation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtCommentBox.Enabled = false;
txtEmailBox.Enabled = false;
txtNameBox.Enabled = false;
txtPhoneBox.Enabled = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("JoelsDefaultPage.aspx");
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Enabled == true)
txtCommentBox.Enabled = true;
else
txtCommentBox.Enabled = false;
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox2.Enabled == true)
txtEmailBox.Enabled = true;
else
txtEmailBox.Enabled = false;
}
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox3.Enabled == true)
txtNameBox.Enabled = true;
else
txtNameBox.Enabled = false;
}
protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox4.Enabled == true)
txtPhoneBox.Enabled = true;
else
txtPhoneBox.Enabled = false;
}
protected void txtCommentBox_TextChanged(object sender, EventArgs e)
{
}
protected void txtEmailBox_TextChanged(object sender, EventArgs e)
{
}
protected void txtNameBox_TextChanged(object sender, EventArgs e)
{
}
protected void txtPhoneBox_TextChanged(object sender, EventArgs e)
{
}
}
编辑#1
好的,所以 AutoPostBack 可以正常工作了。我现在可以从禁用状态启用文本框,但我无法再次禁用它。
更新#1
我已将代码更新为我们现在拥有的代码。
最佳答案
我觉得是这条线有问题
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
缺少 autopostback="true"
结合起来就是
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
编辑-1
这是同一个
的 MSDN 示例
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.autopostback.aspx
编辑-2
但我会建议您使用 java-script
或 jQuery
。
这是一个很好的例子
关于c# - 在 ASP.NET 中使用复选框启用文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575686/