Enabling a Textbox using a checkbox in ASP.NET? [Resolved]

Posted by Kishore22 under ASP.NET on 9/18/2013 | Points: 10 | Views : 20412 | Status : [Member] | Replies : 6
if(cb1.checked=true)
tb1.visible=true;
else
tb1.visible=false;



above code is not working properly can you give me a suggestion please give me a answer




Responses

Posted by: Satyapriyanayak on: 9/18/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" .WebForm2" %>


<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb1" runat="server"></asp:TextBox>
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace
{
public partial class WebForm2 : System.Web.UI.Page
{

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if(CheckBox1.Checked==true)

{
tb1.Visible = true;
}
else
{
tb1.Visible=false;
}

}

protected void Page_Load(object sender, EventArgs e)
{
tb1.Visible = false;
}
}
}



If this post helps you mark it as answer
Thanks

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
To check the state of a checkbox, you want to be using
textbox.Enabled = checkBox.Checked;

Rather than the enabled state of the checkbox.

References:
http://stackoverflow.com/questions/14575686/enabling-a-textbox-using-a-checkbox-in-asp-net
http://stackoverflow.com/questions/3801970/disable-enable-element-with-checkbox-and-jquery

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Enable TextBox with Checkbox Checked using JavaScript
<script type="text/javascript" language="javascript">

function enableTextBox() {
var textBoxID = "<%= txtAddress.ClientID %>";
if (document.getElementById("<%= chkAssociation.ClientID %>").checked == true)
document.getElementById(textBoxID).disabled = false;
else
document.getElementById(textBoxID).disabled = true;
}
</script>
and than the control:
<asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation" runat="server" />
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />


You'll have to look it up really how the client ID is fetched from the control on the server side. It could be a property like I indicated, but might as well be a method. Main point is though that the ID attribute in the server side asp:textbox and asp:checkbox tags is NOT the same as the ID attribute that gets output into the rendered HTML.

Reference:
http://www.aspdotnet-suresh.com/2013/03/javascript-enable-or-disable-controls.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/18/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Kishor,

Please change you code as below:-

if(cb1.checked== true)
tb1.visible=true;
else
tb1.visible=false;

Happy Coding

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/18/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear kishor,

If your problem is solved, then Click on MARK IT AS ANSWER link.

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Kishore,
You can do the same by using javascript functions at client side....
Refer the same in the reply Posted on: 9/18/2013 1:26:09 AM
call the enableTextBox() function in the codebehind


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextBoxAndCheckBox.aspx.cs" Inherits="CopyDataFromOneControlToAnother.TextBoxAndDropDownList" %>

<!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></title>
<script type="text/javascript" language="javascript">
function enableTextBox() {
var textBoxID = "<%= txtAddress.ClientID %>";
if (document.getElementById("<%= chkAssociation.ClientID %>").checked == true)
document.getElementById(textBoxID).disabled = false;
else
document.getElementById(textBoxID).disabled = true;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation" runat="server" />
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />
</div>
</form>
</body>
</html>


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response