Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Standard Web Server Controls

Standard Web Server Controls

The standard set of web server controls that comes with ASP.NET mirrors the HTML server controls in many ways. However, web server controls offer some new refinements and enhancements, such as support for events and view state, a more consistent set of properties and methods, and more built-in functionality. In this section, we’ll take a look as some of the controls you’re most likely to use in your day-to-day work.

Remember to use the .NET Framework 2.0 SDK Documentation whenever you need more details about any of the framework’s classes (or controls). Access the documentation from Start > All Programs > Microsoft .NET Framework SDK v2.0

> Documentation. To find a class, simply search for the class’s name. If there are many classes with a given name in different namespaces, you’ll be able to choose the one you want from the Index Results window. For example, you’ll find that there are three classes named Label, situated in the System.Web.UI.MobileControls, System.Web.UI.WebControls, and System.Windows.Forms namespaces, as Figure 4.3 illustrates. You’ll most likely be interested in the version of the class situated in the WebControls namespace.

Figure 4.3. Documentation for the Label control

103

Chapter 4: Constructing ASP.NET Web Pages

Label

The easiest way to display static text on your page is simply to add the text to the body of the page without enclosing it in any tag. However, if you want to modify the text displayed on a page using ASP.NET code, you can display your text within a Label control. Here’s a typical example:

<asp:Label id="messageLabel" Text="" runat="server" />

The following code sets the Text property of the Label control to display the text “Hello World”:

Visual Basic

Public Sub Page_Load()

messageLabel.Text = "Hello World"

End Sub

C#

public void Page_Load()

{

messageLabel.Text = "Hello World";

}

Reading this Page_Load handler code, we can see that when the page first loads, the Text property of the Label control with the id of message will be set to “Hello World.”

Literal

This is perhaps the simplest control in ASP.NET. If you set Literal's Text property, it will simply insert that text into the output HTML code without altering it. Unlike Label, which has similar functionality, Literal doesn’t wrap the text in <span> tags that would allow the setting of style information.

TextBox

The TextBox control is used to create a box in which the user can type or read standard text. Using the TextMode property, this control can be set to display text in a single line, across multiple lines, or to hide the text being entered (for instance, in HTML password fields). The following code shows how we might use it in a simple login page:

104

Standard Web Server Controls

<p>

Username: <asp:TextBox id="userTextBox" TextMode="SingleLine" Columns="30" runat="server" />

</p>

<p>

Password: <asp:TextBox id="passwordTextBox" TextMode="Password" Columns="30" runat="server" />

</p>

<p>

Comments: <asp:TextBox id="commentsTextBox" TextMode="MultiLine" Columns="30" Rows="10" runat="server" />

</p>

In each of the instances above, the attribute TextMode dictates the kind of text box that’s to be rendered.

HiddenField

HiddenField is a simple control that renders an input element whose type attribute is set to hidden. We can set its only important property, Value.

Button

By default, the Button control renders an input element whose type attribute is set to submit. When a button is clicked, the form containing the button is submitted to the server for processing, and both the Click and Command events are raised.

The following markup displays a Button control and a Label:

<asp:Button id="submitButton" Text="Submit" runat="server" OnClick="WriteText" />

<asp:Label id="messageLabel" runat="server" />

Notice the OnClick attribute on the control. When the button is clicked, the Click event is raised and the WriteText subroutine is called. The WriteText subroutine will contain the code that performs the intended function for this button, such as displaying a message to the user:

Visual Basic

Public Sub WriteText(s As Object, e As EventArgs)

messageLabel.Text = "Hello World"

End Sub

105

Chapter 4: Constructing ASP.NET Web Pages

C#

public void WriteText(Object s, EventArgs e)

{

messageLabel.Text = "Hello World";

}

It’s important to realize that events are associated with most web server controls, and the basic techniques involved in using them, are the same events and techniques we used with the Click event of the Button control. All controls implement a standard set of events because they all inherit from the WebControl base class.

ImageButton

An ImageButton control is similar to a Button control, but it uses an image that we supply in place of the typical system button graphic. Take a look at this example:

<asp:ImageButton id="myImgButton" ImageUrl="myButton.gif" runat="server" OnClick="WriteText" />

<asp:Label id="messageLabel" runat="server" />

The Click event of the ImageButton receives the coordinates of the point at which the image was clicked:

Visual Basic

Public Sub WriteText(s As Object, e As ImageClickEventArgs) messageLabel.Text = "Coordinate: " & e.X & "," & e.Y

End Sub

C#

public void WriteText(Object s, ImageClickEventArgs e)

{

messageLabel.Text = "Coordinate: " + e.X + "," + e.Y;

}

LinkButton

A LinkButton control renders a hyperlink that fires the Click event when it’s clicked. From the point of view of ASP.NET code, LinkButtons can be treated in much the same way as buttons, hence the name.

<asp:LinkButton id="myLinkButon" Text="Click Here" runat="server" />

106