[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ASP.NET Enable/Disbale Textbox based on Checkbox

Posted on 2009-05-16
2
Medium Priority
?
910 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

650 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