C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » JavaScript »

javascript function enable,disable the textbox, if i click a checkbox?


Posted Date: 04 Jul 2008      Posted By: Hari      Member Level: Silver     Points: 1   Responses: 2



Hi,
In my page i have checkbox(its <asp:checkbox>)and when i select the checkbox textbox1 should be enabled and when unchecked the check box the textbox1 should be disabled. This should be done at client side using javascript. Any sample code?





Responses

Author: Shanmugam    04 Jul 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" type="text" runat="server" />
<input id="Checkbox1" type="checkbox" onclick="CHangeStatus()" />
</div>
</form>
<script language="javascript">
function CHangeStatus()
{

document.getElementById ('<%=Text1.ClientID %>').disabled=document.form1.Checkbox1.checked
}
</script>
</body>
</html>



Author: Hubli Sunil    04 Jul 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 6

Hi,

You can write the code as follows,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>
<script language="javascript">
var txtBoxClientId = "<%= txtBox.ClientID.ToString()%>";
function Check(a)
{
if (a.checked == true)
document.getElementById(txtBoxClientId).disabled = false;
else
document.getElementById(txtBoxClientId).disabled = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
<asp:CheckBox ID="chkBox" runat="server" Checked="true"/>
</div>
</form>
</body>
</html>

in the codebehind file write as follows in the pageload event,
protected void Page_Load(object sender, EventArgs e)
{
chkBox.Attributes.Add("onclick", "Check(this);");
}



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : calculate age in javascript
Previous : Positioning Problem: Java Script
Return to Discussion Forum
Post New Message
Category: JavaScript

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use