2 Replies - 4880 Views - Last Post: 28 January 2011 - 08:42 PM Rate Topic: -----

#1 stephen.madden   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-May 09

Function for text box being enabled

Posted 28 January 2011 - 10:04 AM

So i have a check box and a text box. When check box checked, I want the text box to be enabled. Here is what I have so far. I am knew to ASP, so any help is appreciated in understanding what I am doing wrong.

<asp:TextBox ID="cbTerm" runat="server" Text="Terminated" OnCheckBoxChanged="termChecked" />

Then of course the script:
<script type = "text/VB" runat="server">
Function termChecked() As Boolean
If cbTerm.Checked = True Then
End If
End Function

Is This A Good Question/Topic? 0
  • +

Replies To: Function for text box being enabled

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Function for text box being enabled

Posted 28 January 2011 - 11:09 AM

Personally, I would use javascript for this. To keep your code like it is, you would need to do a postback to the server, which would be inefficient to simply enable/disable a textbox.

This isn't tested, but it should work...

<asp:CheckBox ID="cbTerm" runat="server" Text="Terminated" onchange="toggleControl();" />
<asp:TextBox ID="txtTerm" runat="server" />

Noticed that I added the onchange attribute to the control. You can use this attribute to execute javascript code when the checkbox's checked property changes.

then the javascript
    function toggleControl(){
         var chkBox = document.getElementId("<%= cbTerm.ClientID %>");
         var txtBox = document.getElementId("<%= txtTerm.ClientID %>");

         txtBox.disabled = !chkBox.checked;

So our javascript gets the two controls from the page, and sets the disabled property of the textbox to the opposite of the checked property of the checkbox. So when the checkbox is checked, the textbox is not disabled.
Was This Post Helpful? 1
  • +
  • -

#3 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Function for text box being enabled

Posted 28 January 2011 - 08:42 PM



<asp:TextBox ID="cbTerm" runat="server" Text="Terminated" OnCheckBoxChanged="termChecked" />

TextBox does not have an OnCheckBoxChanged event. Not unless you've created a composite control combining the two together. For now i'll assume that's not the case and that they are actually two separate controls. To enable your checkbox to fire the oncheckchanged event you need to enable autopostback. Then in it's code you just enable the text box if the checkbox is checked.

so in your aspx file you would have something like:
            <asp:CheckBox ID="CheckBox1" runat="server" 
                AutoPostBack="true" />
            <asp:TextBox ID="TextBox1" runat="server" Enabled="false"></asp:TextBox>

And in your code behind:
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
                if (CheckBox1.Checked) TextBox1.Enabled = true;
                else TextBox1.Enabled = false;

Edit: my example is in C# but it's pretty much the same logic as you have in your VB. I think you're main problem is where you put the oncheckedchanged event

This post has been edited by Nakor: 28 January 2011 - 08:45 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1