C# 是否在ASP.NET中使用复选框启用文本框?
我们已尝试使用C#将复选框1链接到txtCommentBox。我们正试图使txtCommentBox保持禁用状态,直到选中复选框1 我们已经完成了以下工作C# 是否在ASP.NET中使用复选框启用文本框?,c#,asp.net,.net,checkbox,textbox,C#,Asp.net,.net,Checkbox,Textbox,我们已尝试使用C#将复选框1链接到txtCommentBox。我们正试图使txtCommentBox保持禁用状态,直到选中复选框1 我们已经完成了以下工作 if (checkBox1.Enabled) { txtCommentBox.Enabled = true; } txtCommentBox.Enabled = checkBox1.Enabled; 失败后,我们在page_Load方法中尝试执行以下操作 if (checkBox1.Enabled) { txtCommen
if (checkBox1.Enabled)
{
txtCommentBox.Enabled = true;
}
txtCommentBox.Enabled = checkBox1.Enabled;
失败后,我们在page_Load方法中尝试执行以下操作
if (checkBox1.Enabled)
{
txtCommentBox.Enabled = true;
}
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>
编辑#1
好的,所以自动回邮系统可以正常工作。我现在可以从禁用状态启用文本框,但是我不能再次禁用它
更新#1
我已经将代码更新到现在的版本。我认为问题在于这一行
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
编辑-2
但是我建议您使用java脚本
或jQuery
进行同样的操作。
这是一个很好的例子
您必须将“AutoPostBack”设置为true,以便在选中该复选框时,它将重新加载页面,并在if语句中运行your命令。这对我有效。。也许你在所有的事件中都错过了另一个。正如@MsB所指出的,每当您想回发到服务器时,您还需要将checkbox的AutoPostBack属性设置为true。。。检查下面的示例(也通过javascript更新了我的答案以启用/禁用)
通过服务器端的复选框启用/禁用
姓名:
通过客户端复选框启用/禁用
地址:
函数EnableDisableAddress(){
var chkAddress=document.getElementById(“”);
var txtAddress=document.getElementById(“”);
txtAddress.disabled=!chkAddress.checked;
}
受保护的void cbEnableName_CheckedChanged(对象发送方,事件参数e)
{
如果(cbEnableName.Checked)
txtName.Enabled=true;
其他的
txtName.Enabled=false;
}
要检查复选框的状态,请使用
textbox.Enabled = checkBox.Checked;
而不是复选框的启用状态。protectedvoid EnableTextBox()
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();
}
{
int count=int.Parse(GridView1.Rows.count.ToString());
for(int i=0;i
您希望在客户端还是回发时完成?我们正在考虑回发。我认为您应该创建一个ViewState属性,并在其中存储复选框的值。在回发时,您可以将ViewState中的值设置为textbox,例如ViewState1=checkBox1.Enabled(在C#文件中)txtCommentBox=ViewState1(在回发中)?回发是如何发生的?我同意,使用javascript可能会更容易。然而,我们正试图在C#中实现这一点,因为这是项目要求。我会给自动回邮打一针,并将结果更新给你。我会给它打一针,我会让你知道结果。好的,谢谢。我还将对此进行一次尝试,并根据结果进行更新。值得注意的是,在服务器端处理它的属性是OnCheckedChanged
,而在客户端处理它的属性是OnClick
。我在试验时并没有注意到这一点,我浪费时间试图找出我的客户端代码不起作用的原因。无论如何,在服务器或客户机上都有很好的例子,这就解决了问题。非常感谢你。没问题,你一定要考虑做这个客户端,尽管正如另一个家伙建议的那样,会改善用户体验。
textbox.Enabled = checkBox.Checked;
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();
}