我们尝试使用 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
我已经将代码更新为我们现在所拥有的。