473,465 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

34 New Member
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
2 2760
Frinavale
9,735 Recognized Expert Moderator Expert
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
This is possible with JavaScript.
Just remember that JavaScript works on Html objects, not ASP objects.

You'll need to know the ClientId of the Textbox control so that you can access it using yur JavaScript....

So basically you're going to write a JavaScript function that handles the onClick event of the check box and is passed the name (clientID) of the textbox.... then enable the textbox or disable it depending on the value of the check box by using the getElementByID() javascript method.

Take a look at the .NET article How to check if a textbox contains a number to get a better idea of how to use JavaScript in your .NET project.

-Frinny
Aug 10 '07 #2
JLC
34 New Member
Well I think I sort of got it, but it still isn't working correctly. I have it so when the page loads, the check box is checked. And when you uncheck the check box, the text becomes disabled. But when you recheck the check box, nothing happens.

Here's my code...I'm not sure why I am not getting this to work.
Thanks...

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NotifyDialog.aspx.cs" Inherits="NotifyDialog" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Notify Dialog Page</title>
  8. <script type="text/javascript">
  9.     function EmailChecked(checkboxid)
  10.     {     
  11.         if(checkboxid.defaultChecked == true)
  12.         {
  13.             document.getElementById('EmailTextBox').disabled=true;
  14.         }
  15.         else
  16.         {
  17.             document.getElementById('EmailTextBox').disabled=false;
  18.         }
  19.  
  20.     }
  21. </script>
  22.  
  23. </head>
  24.  
  25. <body>
  26. <form action="NotifyDialog.aspx" runat="server">
  27.    <div style="width: 525px; height: 300px">
  28.  
  29.    <!-- will eventually pull items from Davids db, but will hard code them for now -->
  30.     <asp:DropDownList ID="StatusDropDownList" runat="server" style="z-index: 100; left: 39px; position: absolute; top: 62px" Width="158px" >
  31.         <asp:ListItem Selected="True" Text="Released" Value="Released">Released</asp:ListItem>
  32.         <asp:ListItem Text="Down" Value="Down">Down</asp:ListItem>
  33.         <asp:ListItem Text="Blocked" Value="Blocked">Blocked</asp:ListItem>
  34.         <asp:ListItem Text="Deploying" Value="Deploying">Deploying</asp:ListItem>
  35.     </asp:DropDownList>
  36.  
  37.  
  38.  
  39.     <asp:CheckBox id="EmailCheckBox" runat="server" checked="true" style="z-index: 101; left: 39px; position: absolute; top: 104px" />
  40.  
  41.     <asp:TextBox ID="EmailTextBox" runat="server" text="Select an option from the Status drop down menu" style="z-index: 102; left: 39px; position: absolute; top: 131px" Height="103px" TextMode="MultiLine" Width="440px"></asp:TextBox>
  42.  
  43.     <asp:Button ID="SubmitButton" runat="server" Text="Submit" style="z-index: 103; left: 435px; position: absolute; top: 252px" Width="50px" />
  44.  
  45.     <asp:Label ID="EmailLabel" runat="server" Style="z-index: 104; left: 66px; position: absolute;
  46.            top: 105px" Text="Send Email"></asp:Label>
  47.     <asp:Label ID="StatusLabel" runat="server" Style="z-index: 106; left: 39px; position: absolute;
  48.            top: 36px" Text="Status"></asp:Label>
  49.  
  50.    </div>
  51.    </form>
  52.  
  53. </body>
  54. </html>
  55.  

Here's for the .cs page

Expand|Select|Wrap|Line Numbers
  1. public partial class NotifyDialog : System.Web.UI.Page
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.  
  6.        EmailCheckBox.Attributes.Add("OnClick", "javascript: EmailChecked(" + EmailCheckBox.ClientID + ")");
  7.     }
  8.  
This is possible with JavaScript.
Just remember that JavaScript works on Html objects, not ASP objects.

You'll need to know the ClientId of the Textbox control so that you can access it using yur JavaScript....

So basically you're going to write a JavaScript function that handles the onClick event of the check box and is passed the name (clientID) of the textbox.... then enable the textbox or disable it depending on the value of the check box by using the getElementByID() javascript method.

Take a look at the .NET article How to check if a textbox contains a number to get a better idea of how to use JavaScript in your .NET project.

-Frinny
Aug 13 '07 #3

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

Similar topics

0
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...
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...
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: 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: ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.