Solved

ASP.NET Enable/Disbale Textbox based on Checkbox

Posted on 2009-05-16
2
906 Views
Last Modified: 2012-05-07
I have two .Net controls on a form (textbox and checkbox), how can i disable the textbox when the checkbox is checked and also enable it if the user unchecks it?

I dont want to do a postback so I assume this needs to be done via Javascript.
0
Comment
Question by:ITHelper80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
rdogmartin earned 500 total points
ID: 24404947
This solution assumes you have MS AJAX. First, add a click event handler to the checkbox control:

<asp:CheckBox ID="chk1" runat="server" onclick="chk1_Click()" Text="My checkbox" />

Then implement your event handler:

 function chk1_Click()
{
  handleCheckBoxClick();
}

function handleCheckBoxClick()
{
  var chkbx = $get('<%= chk1.ClientID %>');
  var txtbx = $get('<%= txtbx.ClientID %>');

  txtbx.disabled = (!chkbx.checked || chkbx.disabled);
}

You may need to disable the textbox when the page first loads. To do this, add a function that runs after page load:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(myPageLoad);

function myPageLoad(sender, args)
{
  handleCheckBoxClick();
}




0
 
LVL 6

Expert Comment

by:rdogmartin
ID: 24404950
I should also mention that disabled controls do not post their state during postbacks, so you will not be able to read the text in a disabled textbox on the server side. Hopefully this isn't a requirement.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question