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

Chapter 4: Constructing ASP.NET Web Pages

<asp:Panel id="myPanel" runat="server">

<p>Username: <asp:TextBox id="usernameTextBox" Columns="30" runat="server" /></p>

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

</p>

</asp:Panel>

<asp:Button id="hideButton" Text="Hide Panel" OnClick="HidePanel" runat="server" />

The code above places two TextBox controls within a Panel control. The Button control is outside of the panel. The HidePanel subroutine would then control the Panel’s visibility by setting its Visible property to False:

Visual Basic

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

myPanel.Visible = False

End Sub

C#

public void HidePanel(Object s, EventArgs e)

{

myPanel.Visible = false;

}

In this case, when the user clicks the button, the Click event is raised and the HidePanel subroutine is called, which sets the Visible property of the Panel control to False.

List Controls

Here, we’ll meet the ASP.NET controls that display simple lists of elements:

ListBox, DropDownList, CheckBoxList, RadioButtonList, and BulletedList.

DropDownList

A DropDownList control is similar to the HTML select element. The DropDownList control allows you to select one item from a list using a drop-down menu.

<asp:DropDownList id="ddlFavColor" runat="server"> <asp:ListItem Text="Red" value="red" /> <asp:ListItem Text="Blue" value="blue" />

110

List Controls

<asp:ListItem Text="Green" value="green" /> </asp:DropDownList>

The most useful event that this control provides is SelectedIndexChanged. This event is exposed by other list controls, such as the CheckBoxList and RadioButtonList controls, allowing for easy programmatic interaction with the control. These controls can also be bound to a database, allowing you to extract dynamic content into a drop-down menu.

ListBox

A ListBox control equates to the HTML select element with either the multiple or size attribute set (size would need to be set to a value of 2 or more). If you set the SelectionMode attribute to Multiple, the user will be able to select more than one item from the list, as in this example:

<asp:ListBox id="listTechnologies" runat="server" SelectionMode="Multiple">

<asp:ListItem Text="ASP.NET" Value="aspnet" /> <asp:ListItem Text="JSP" Value="jsp" /> <asp:ListItem Text="PHP" Value="php" /> <asp:ListItem Text="CGI" Value="cgi" /> <asp:ListItem Text="ColdFusion" Value="cf" />

</asp:ListBox>

RadioButtonList

Like the RadioButton control, the RadioButtonList control represents radio buttons. However, the RadioButtonList control represents a list of radio buttons and uses more compact syntax. Here’s an example:

<asp:RadioButtonList id="favoriteColor" runat="server"> <asp:ListItem Text="Red" Value="red" />

<asp:ListItem Text="Blue" Value="blue" /> <asp:ListItem Text="Green" Value="green" />

</asp:RadioButtonList>

CheckBoxList

As you may have guessed, the CheckBoxList control represents a group of check boxes; it’s equivalent to using several CheckBox controls in row:

<asp:CheckBoxList id="favoriteFood" runat="server"> <asp:ListItem Text="Pizza" Value="pizza" />

111