473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

want to show/hide text in asp:textbox depending on if asp:checkbox is checked

34
Hi,

I am creating a page in asp.net that has a checkbox and a textbox. When the checkbox is checked I want the textbox to become active and show text. If the checkbox is unchecked, I would like the text box to become disabled and show no text.

Is this possible to do via javascript? I would like to handle it all client side.

Thanks,
JLC
Aug 9 '07 #1
8 4920
acoder
16,027 Expert Mod 8TB
Yes, this is possible with Javascript. What code do you have so far?

See the checkbox object and input text object. You will need to handle the onclick on the checkbox and set the disabled property of the input text box.
Aug 10 '07 #2
JLC
34
Hi,

Thanks for the links.
This is the code I have so far, but so far it's not working.
What I want to do is if the check box is checked I want to show some boilerplate text that will be set by the choice in the dropdownlist box. But I was trying to just get the checkbox to show or not show text at all before moving on to what I described above.

Here's the code in my .cs file.
Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.        EmailCheckBox.Attributes.Add("OnClick", "javascript: EmailChecked(" + EmailCheckBox.Checked + ")");
  4.     }
  5.  
and here's the code in my aspx page
Expand|Select|Wrap|Line Numbers
  1.   function EmailChecked(checkbox, textboxid)
  2.     {
  3.         var textbox = document.getElementById('EmailTextBox');
  4.         if(checkbox.checked)
  5.         {
  6.             textbox.style.display = '';
  7.         }
  8.         else
  9.         {
  10.             textbox.style.display = 'none';
  11.         }
  12.  
  13.     }
  14.  
  15.  
  16.  
Thanks!
JLC

Yes, this is possible with Javascript. What code do you have so far?

See the checkbox object and input text object. You will need to handle the onclick on the checkbox and set the disabled property of the input text box.
Aug 13 '07 #3
JLC
34
Ok I am scrapping that last post...

This is now my code...I just want to enable/disable the text box based on if the check box is checked.
here's the code:

.cs file
Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.        EmailCheckBox.Attributes.Add("OnClick", "javascript: EmailChecked(" + EmailCheckBox.Checked + ")");
  4.     }
  5.  
and the .aspx file

Expand|Select|Wrap|Line Numbers
  1.     function EmailChecked(checkbox)
  2.     {     
  3.         if(checkbox.defaultChecked = true)
  4.         {
  5.             document.getElementById('EmailTextBox').disabled=false;
  6.         }
  7.         else
  8.         {
  9.             document.getElementById('EmailTextBox').disabled=true;
  10.         }
  11.  
  12.     }
  13.  
But when I run the page, and click the check box, I get an error that says:

"False is undefined" for the line in my html that is for the checkbox.

what am I doing wrong??

Thanks,
j.

Hi,

Thanks for the links.
This is the code I have so far, but so far it's not working.
What I want to do is if the check box is checked I want to show some boilerplate text that will be set by the choice in the dropdownlist box. But I was trying to just get the checkbox to show or not show text at all before moving on to what I described above.

Here's the code in my .cs file.
Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.        EmailCheckBox.Attributes.Add("OnClick", "javascript: EmailChecked(" + EmailCheckBox.Checked + ")");
  4.     }
  5.  
and here's the code in my aspx page
Expand|Select|Wrap|Line Numbers
  1.   function EmailChecked(checkbox, textboxid)
  2.     {
  3.         var textbox = document.getElementById('EmailTextBox');
  4.         if(checkbox.checked)
  5.         {
  6.             textbox.style.display = '';
  7.         }
  8.         else
  9.         {
  10.             textbox.style.display = 'none';
  11.         }
  12.  
  13.     }
  14.  
  15.  
  16.  
Thanks!
JLC
Aug 13 '07 #4
acoder
16,027 Expert Mod 8TB
You're passing EmailCheckbox.Checked which is either true or false. In the function, you just need to check whether it's equal to true or false. Don't forget to use double equals (==).
Aug 13 '07 #5
JLC
34
Ok so I changed it to this:

Expand|Select|Wrap|Line Numbers
  1.     function EmailChecked(checkboxid)
  2.     {     
  3.         if(checkboxid.defaultChecked == true)
  4.         {
  5.             document.getElementById('EmailTextBox').disabled=true;
  6.         }
  7.         else
  8.         {
  9.             document.getElementById('EmailTextBox').disabled=false;
  10.         }
  11.  
  12.     }
  13.  
and it works when I uncheck the box, the text becomes disabled. But when I check the checkbox right after that, it doesn't do anything. I can no longer get the text to become enabled again. Why is that?
Thanks!

You're passing EmailCheckbox.Checked which is either true or false. In the function, you just need to check whether it's equal to true or false. Don't forget to use double equals (==).
Aug 13 '07 #6
acoder
16,027 Expert Mod 8TB
Ok so I changed it to this:

Expand|Select|Wrap|Line Numbers
  1.     function EmailChecked(checkboxid)
  2.     {     
  3.         if(checkboxid.defaultChecked == true)
  4.         {
  5.             document.getElementById('EmailTextBox').disabled=true;
  6.         }
  7.         else
  8.         {
  9.             document.getElementById('EmailTextBox').disabled=false;
  10.         }
  11.  
  12.     }
  13.  
and it works when I uncheck the box, the text becomes disabled. But when I check the checkbox right after that, it doesn't do anything. I can no longer get the text to become enabled again. Why is that?
Thanks!
You're checking the 'defaultChecked' property. You need to check the 'checked' property instead (a bit of a mouthful that!).
Aug 13 '07 #7
JLC
34
Ok I got it...thanks so much for the help!
JLC

You're checking the 'defaultChecked' property. You need to check the 'checked' property instead (a bit of a mouthful that!).
Aug 13 '07 #8
acoder
16,027 Expert Mod 8TB
Ok I got it...thanks so much for the help!
JLC
No problem. You're welcome.
Aug 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Maziar Aflatoun | last post by:
Hi everyone, I am reading and displaying data rows from my database where the first column contains the Status checkbox. I like to enable my users to change the status of individual rows by...
1
by: Liu Xuesong | last post by:
I want to add Attributes into CheckBox in .aspx page <html> <asp:CheckBox id="ChapterCheck" runat="server" /> </html> How to add Attributes in <asp:CheckBox> ?
3
by: C | last post by:
Hi, I have an asp server side checkbox as below. I have a page where users can edit their user record. I set this checkbox to true based on a value in a database. <asp:checkbox...
9
by: Harry | last post by:
Dear All, First of all, i have a database and i have to select the data in a table. In the web form, i have a checkbox in each rows. So that the use can select the row. initally, i am...
2
by: Maziar Aflatoun | last post by:
Hi everyone, I am reading and displaying data rows from my database where the first column contains the Status checkbox. I like to enable my users to change the status of individual rows by...
3
by: Jim Bancroft | last post by:
Hi everyone, This is a silly one I'm sure, but I'm having a whale of a time putting some text into my asp checkbox. Here's what I'd like to do: <asp:CheckBox runat="server"...
2
by: UJ | last post by:
Is there a way with a asp:checkbox to run a JavaScript to display/hide text/input on the screen without doing a postback? I also need to be able to access the stuff at the server so I need to...
1
by: UJ | last post by:
I have a table that I need to manually generate based on stuff in the database. I build the actual table string and stuff it into an asp:panel. So far it works fine. I'm now trying to add a...
4
by: Mike Haberfellner | last post by:
....hi everyone, ....i posted this allready, but my newsreader doesn't display it to me - so i'm sorry if it's posted twice... hi again, sorry for not clearly writing what i really need :) ...
1
by: mark4asp | last post by:
Here is the control: <asp:CheckBox ID="chkLite" runat="server" Checked="False" AutoPostBack="True" OnCheckedChanged="chkLite_CheckedChanged" /> Here is the code-behind: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.