Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enable button with a checkbox VB

Posted on 2008-06-17
4
Medium Priority
?
2,138 Views
Last Modified: 2010-04-21
i am using VB Script. i have a check box and a disabled button. i wish to enable button when checkbox is checked and disable button when checkbox is unchecked. Here is the code I have so far:

Default.aspx
<asp:CheckBox ID="CBFlights" runat="server" Text="Flights" value="ON" onclick="enbutton()"/>
<asp:Button ID="BNFlight" runat="server" Text="Flights" Width="74px"  Enabled="False" />

Default.aspx.vb
Sub enbutton()
        If CBFlights.Checked = True Then
            BNFlight.Enabled = True
        Else
            BNFlight.Enabled = False
        End If
    End Sub

Can anyone help here - not sure what I am doing wrong :(
Thanks
0
Comment
Question by:Phesant123
[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
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 21801669
Hello Phesant123,

Firstly what you really want to be doing is to do this on the client-side and not on the server-side. For a simple task like this there is really no need to make a round-trip to the server.

Use the following code (assuming you are using framework 2.0 or higher, let me know if you would prefer a 1.0/1.1 version).

<asp:CheckBox ID="CBFlights" runat="server" Text="Flights" value="ON" onclientclick="document.getElementById('<%=BNFlight.ClientID()%>').disabled=!this.checked;"/>
<asp:Button ID="BNFlight" runat="server" Text="Flights" Width="74px"  Enabled="False" />

I know this is a javascript version but it is much simpler than writing a seperate function when it is not really necessary.

Regards,

TimCottee
0
 
LVL 1

Author Comment

by:Phesant123
ID: 21801721
Hi TimCottee

Thanks for your speedy reply - thats exactly what I require above but I have entered the code above:

<asp:CheckBox ID="CBFlights" runat="server" Text="Flights" value="ON" onclientclick="document.getElementById('<%=BNFlight.ClientID()%>').disabled=!this.checked;"/>
 <asp:Button ID="BNFlight" runat="server" Text="Flights" Width="74px"  Enabled="False" />

The button remains blanked out when I click in the checkbox - any ideas?
Thanks
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 2000 total points
ID: 21801983
Phesant123,

Seems that the checkbox for some reason doesn't have the OnClientClick attribute, you will have to do it the other way:

            CBFlights.Attributes.Add("onclick", String.Format("document.getElementById('{0}').disabled=!this.checked;",BNFlight.ClientId))

Put this in your page_load event

TimCottee
0
 
LVL 1

Author Closing Comment

by:Phesant123
ID: 31467908
Thanks TimCottee
That works perfect
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 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