我们尝试使用C#将checkBox1链接到txtCommentBox。我们正在努力使其在检查checkBox1之前保持禁用txtCommentBox。
我们已经完成了以下工作。
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
我已将代码更新为现在的内容。
答案 0 :(得分:4)
我认为问题在于这一行
<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" />
这是同一个的MSDN示例 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.autopostback.aspx
但我建议您使用java-script
或jQuery
这是一个很好的例子
答案 1 :(得分:3)
这对我有用..也许你在所有活动中都缺少其他的东西。正如@MsB指出的那样,每当你想要回发到服务器时,你还需要将checkbox的AutoPostBack属性设置为true ...检查下面的示例(更新了我的答案,通过javascript启用/禁用)强>
<form id="form1" runat="server">
<div>
<h2>Enable/Disable via checkbox server side</h2>
<p>
<label>Name:</label>
<asp:TextBox ID="txtName" runat="server" Enabled="false"></asp:TextBox>
<asp:CheckBox ID="cbEnableName" runat="server" AutoPostBack="true"
Text="Enable Name" oncheckedchanged="cbEnableName_CheckedChanged" />
</p>
<h2>Enable/Disable via checkbox client side</h2>
<p>
<label>Address:</label>
<asp:TextBox ID="txtAddress" runat="server" Enabled="false"></asp:TextBox>
<asp:CheckBox ID="cbEnableAddress" runat="server" onclick="EnableDisableAddress()"
Text="Enable Address" />
</p>
</div>
<script type="text/javascript">
function EnableDisableAddress() {
var chkAddress = document.getElementById('<%=cbEnableAddress.ClientID %>');
var txtAddress = document.getElementById('<%= txtAddress.ClientID %>');
txtAddress.disabled = !chkAddress.checked;
}
</script>
</form>
protected void cbEnableName_CheckedChanged(object sender, EventArgs e)
{
if (cbEnableName.Checked)
txtName.Enabled = true;
else
txtName.Enabled = false;
}
答案 2 :(得分:3)
要检查复选框的状态,您希望使用
textbox.Enabled = checkBox.Checked;
而不是复选框的启用状态。
答案 3 :(得分:2)
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
您必须将“AutoPostBack”设置为true,以便在选中该复选框时,它将重新加载页面并在if语句中运行您的命令。
答案 4 :(得分:0)
protected void EnableTextBox()
{
int count = int.Parse(GridView1.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
CheckBox cb2 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox3");
TextBox tb = (TextBox)GridView1.Rows[i].Cells[4].FindControl("txtration");
TextBox tb1 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txtjob");
TextBox tb2 = (TextBox)GridView1.Rows[i].Cells[6].FindControl("txtaadhar");
if (cb.Checked == true)
{
tb.Visible = true;
}
else
{
tb.Visible = false;
}
if (cb1.Checked == true)
{
tb1.Visible = true;
}
else
{
tb1.Visible = false;
}
if (cb2.Checked == true)
{
tb2.Visible = true;
}
else
{
tb2.Visible = false;
}
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
EnableTextBox();
}