Reputation: 113
We have tried to link the checkBox1 to txtCommentBox using C#. We are trying to make it so that the txtCommentBox stays disabled until the checkBox1 is checked.
We've done the below.
if (checkBox1.Enabled)
{
txtCommentBox.Enabled = true;
}
After that failed, we then in the page_Load method, we've tried to do the below.
txtCommentBox.Enabled = checkBox1.Enabled;
That didn't work either. We've tried various properties with the controls with no luck. The .aspx code is below, with the C# code further below.
<%@ 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>
I've put our C# code for what where we're at, below:
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)
{
}
}
EDIT #1
Okay, so the AutoPostBack sort of worked. I can now enable the textbox from its disabled state, however I cannot disable it again.
UPDATE #1
I've updated the code to what we have now.
Upvotes: 2
Views: 20733
Reputation: 1
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();
}
Upvotes: 0
Reputation: 2165
To check the state of a checkbox, you want to be using
textbox.Enabled = checkBox.Checked;
Rather than the enabled state of the checkbox.
Upvotes: 3
Reputation: 13038
This works for me.. Perhaps you are missing an else in all your events. As @MsB pointed out, you would also need to set AutoPostBack property of checkbox to true whenever you want to postback to the server... Check below sample (Updated my answer to enable/disable via javascript as well)
<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;
}
Upvotes: 3
Reputation: 17604
I think the problem is with this line
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
It is missing autopostback="true"
Combining it will be
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
Here is a MSDN example for the same
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.autopostback.aspx
But I will suggest you to use java-script
or jQuery
for the same.
Here is a good example
Disable/enable element with checkbox and jQuery?
Upvotes: 4
Reputation: 1133
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
You must set "AutoPostBack" to true so that, when the checkbox is checked, it will reload the page and will run the your command in your if statement.
Upvotes: 2