JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Immediate Solutions: Enabling and Disabling Controls

To see how to work with a number of important aspects of Web server controls, take a look at the Controls example on the CD-ROM. You can see this example at work in Figure 15.1. You can use the various buttons to work on the text box that appears in the bottom of the page, such as moving the text box to a new position.

Click To expand
Figure 15.1: The Controls example.

Here is Webform1.aspx from this example, the actual Web page sent by the server (after it handles the ASP elements in this page) to the server:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="Controls.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title></title>
<meta content="Microsoft Visual Studio.NET 7.0" name=GENERATOR>
<meta content="Visual Basic 7.0" name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5
name=vs_targetSchema>
  </HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<asp:button id=Button1 style="Z-INDEX: 101; LEFT: 170px;
POSITION: absolute; TOP: 47px" runat="server"
Text="Enable/disable text box" Width="164px" Height="24px"
ToolTip="This is a button"></asp:button>
<asp:Button id=Button4 style="Z-INDEX: 105; LEFT: 190px;
POSITION: absolute; TOP: 165px" runat="server"
Text="Move text box"></asp:Button>
<asp:button id=Button3 style="Z-INDEX: 104; LEFT: 152px;
POSITION: absolute; TOP: 128px" runat="server"
Text="Change text box's style">
</asp:button>
<asp:button id=Button2 style="Z-INDEX: 103; LEFT: 162px;
POSITION: absolute; TOP: 88px" tabIndex=1 runat="server"
Text="Make text box visible/invisible" Width="184px" Height="24px">
</asp:button><asp:textbox id=TextBox1 style="Z-INDEX: 102; LEFT:
181px; POSITION: absolute; TOP: 206px" tabIndex=2 runat="server"
accessKey=T>
This is a text box.</asp:textbox></form>

  </body>
</HTML>

And here is WebForm1.aspx.vb, the Visual Basic code for this Web application:

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents Button3 As System.Web.UI.WebControls.Button
    Protected WithEvents Button4 As System.Web.UI.WebControls.Button
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()

    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles MyBase.Load
         'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Enabled = Not TextBox1.Enabled
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Visible = Not TextBox1.Visible
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Style("BACKGROUND-COLOR") = "aqua"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Style("Left") = "300px"
    End Sub
End Class

The first thing I'll do with this example is show how controls can be enabled and disabled, just as they can in Windows forms. You use the Enabled property to enable or disable controls; when you click the "Enable/disable text box" button, I'll toggle the Boolean value of this property, like this:

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Enabled = Not TextBox1.Enabled
    End Sub

You can see the result in Figure 15.2, where the text box has been disabled; that is, grayed out and made inaccessible so it can no longer accept the keyboard focus, which means the user can't change the text in the control.

Click To expand
Figure 15.2: Disabling a text box.
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor