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

#1 stephen.madden  Icon 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
txtTerm.Enabled=True
Else
txtTerm.Enabled=False
End If
End Function



Is This A Good Question/Topic? 0
  • +

Replies To: Function for text box being enabled

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • 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
<script>
    function toggleControl(){
  
         var chkBox = document.getElementId("<%= cbTerm.ClientID %>");
         var txtBox = document.getElementId("<%= txtTerm.ClientID %>");

         txtBox.disabled = !chkBox.checked;
    }
</script>



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  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Function for text box being enabled

Posted 28 January 2011 - 08:42 PM

First:

Quote

<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" 
                oncheckedchanged="CheckBox1_CheckedChanged"
                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