ASP.NET Tutorial/ASP.net Controls/ListBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:00, 26 мая 2010
Содержание
- 1 asp:ListBox OnSelectedIndexChanged event and AutoPostBack (VB.net)
- 2 Bind array data to asp:ListBox (VB.net)
- 3 Bind asp:Label with asp:ListBox (VB.net)
- 4 Fill value to asp:ListBox in asp:RadioButtonList selection changed action (VB.net)
- 5 Property tester
- 6 Selection mode of asp:ListBox (C#)
- 7 Selection mode of asp:ListBox (VB)
- 8 Set array value to asp:ListBox DataSource (VB.net)
- 9 Using the ListBox control (C#)
- 10 Using the ListBox control (VB)
asp:ListBox OnSelectedIndexChanged event and AutoPostBack (VB.net)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>
<script runat="server">
sub NameHandler(Sender as Object, e as EventArgs)
lblMessage.Text = tbName.Text & ", select a color: "
end sub
sub ListHandler(Sender as Object, e as EventArgs)
lbColor.BackColor = Color.FromName(Sender.SelectedItem.Text)
end sub
</script>
<html><body>
<form runat="server">
<asp:Textbox id="tbName" runat="server"
OnTextChanged="NameHandler"
AutoPostBack="true" />
<asp:Label id="lblMessage" runat="server"
Text="Select a color: " />
<asp:Listbox id="lbColor" runat="server"
OnSelectedIndexChanged="ListHandler"
AutoPostBack="true" >
<asp:Listitem>Red</asp:Listitem>
<asp:Listitem>blue</asp:Listitem>
<asp:Listitem>green</asp:Listitem>
<asp:Listitem>white</asp:Listitem>
</asp:Listbox>
</form>
</body></html>
Bind array data to asp:ListBox (VB.net)
<script runat="server">
dim strName as String = "A"
dim myArray() as String = {"Hello", "World"}
dim myString as String = "AA"
sub Page_Load(Sender as Object, e as EventArgs)
Page.DataBind()
end sub
</script>
<html>
<form runat="server">
My Name is <%# strName %>
<asp:Listbox datasource="<%# myArray %>" runat="server" />
<asp:Textbox text="<%# myString.ToString %>" runat="server" />
</form>
</html>
Bind asp:Label with asp:ListBox (VB.net)
<script language="VB" runat="server">
sub Index_Changed(Sender as Object, e as EventArgs)
DataBind()
end sub
</script>
<html><body>
<form runat="server">
<asp:Listbox runat="server" id="lbColors"
AutoPostBack=true
rows="1"
SelectionMode="Single"
OnSelectedIndexChanged="Index_Changed" >
<asp:Listitem value="1">Red</asp:Listitem>
<asp:Listitem value=" 2">Blue</asp:Listitem>
<asp:Listitem value=" 3">Green</asp:Listitem>
<asp:Listitem value=" 4">Yellow</asp:Listitem>
</asp:Listbox>
<asp:Label id="lblMessage" runat="server"
Text="<%# lbColors.selectedItem.Text %>" />
</form>
</body></html>
Fill value to asp:ListBox in asp:RadioButtonList selection changed action (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
sub ChangeNames(Sender as Object, e as EventArgs)
if rlSex.SelectedIndex = 0 then
"female
lbNames.Items(0).Text = "A"
lbNames.Items(1).Text = "B"
lbNames.Items(2).Text = "C"
lbNames.Items(3).Text = "D"
else
"male
lbNames.Items(0).Text = "E"
lbNames.Items(1).Text = "F"
lbNames.Items(2).Text = "G"
lbNames.Items(3).Text = "H"
end if
end sub
sub DisplayMessage(Sender as Object, e as EventArgs)
lblMessage.Text = Sender.SelectedItem.Text & _
" is a wonderful name!"
end sub
</script>
<html><body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"/>
Choose the sex of your baby:
<asp:RadioButtonList id="rlSex" runat="server"
OnSelectedIndexChanged="ChangeNames"
AutoPostBack="true" >
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
</asp:RadioButtonList>
Possible names:<br>
<asp:ListBox id="lbNames" runat="server"
OnSelectedIndexChanged="DisplayMessage"
AutoPostBack="true" >
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:ListBox>
</form>
</body></html>
Property tester
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PropertyTester" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Server Control Property Tester</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Server Control Property Tester</h1>
<asp:Panel ID="panChange" runat="server"
GroupingText="Controls that will change">
<asp:Label id="labHere" runat="server">Here is a label</asp:Label>
<asp:TextBox id="txtBox" runat="server">Sample Text</asp:TextBox>
<asp:Label id="labAnother" runat="server">This is yet another label</asp:Label>
<asp:Button id="btnSample" runat="server" Text="Sample Button"></asp:Button>
</asp:Panel>
<asp:Panel ID="panProperties" runat="server"
GroupingText="When you choose these display properties">
<br/><asp:CheckBox id="chkVisible" runat="server" Text="Visible" AutoPostBack="True" Checked="True" OnCheckedChanged="chkVisible_CheckedChanged"></asp:CheckBox>
<br/><asp:CheckBox id="chkEnabled" runat="server" Text="Enabled" AutoPostBack="True" Checked="True" OnCheckedChanged="chkEnabled_CheckedChanged"></asp:CheckBox>
<br/>Text Color:
<asp:DropDownList id="drpTextColor" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpTextColor_SelectedIndexChanged">
<asp:ListItem Value="0" Selected="True">Choose a color</asp:ListItem>
<asp:ListItem Value="Black">Black</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="CadetBlue">Cadet Blue</asp:ListItem>
<asp:ListItem Value="Firebrick">Firebrick</asp:ListItem>
<asp:ListItem Value="Gainsboro">Gainsboro</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Gold">Gold</asp:ListItem>
<asp:ListItem Value="LimeGreen">Lime Green</asp:ListItem>
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="SaddleBrown">Saddle Brown</asp:ListItem>
<asp:ListItem Value="Violet">Violet</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
</asp:DropDownList>
<br/>Background Color:
<asp:DropDownList id="drpBackgroundColor" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpBackgroundColor_SelectedIndexChanged">
<asp:ListItem Value="0" Selected="True">Choose a color</asp:ListItem>
<asp:ListItem Value="Black">Black</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="CadetBlue">Cadet Blue</asp:ListItem>
<asp:ListItem Value="Firebrick">Firebrick</asp:ListItem>
<asp:ListItem Value="Gainsboro">Gainsboro</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Gold">Gold</asp:ListItem>
<asp:ListItem Value="LimeGreen">Lime Green</asp:ListItem>
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="SaddleBrown">Saddle Brown</asp:ListItem>
<asp:ListItem Value="Violet">Violet</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
</asp:DropDownList>
<br/>Font:
<asp:DropDownList id="drpFont" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpFont_SelectedIndexChanged">
<asp:ListItem Value="Choose a font">Choose a font</asp:ListItem>
<asp:ListItem Value="Arial">Arial</asp:ListItem>
<asp:ListItem Value="Times New Roman">Times New Roman</asp:ListItem>
<asp:ListItem Value="Tahoma">Tahoma</asp:ListItem>
</asp:DropDownList>
<br/>Font Size:
<asp:DropDownList id="drpFontSize" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpFontSize_SelectedIndexChanged">
<asp:ListItem Value="Choose a font size" Selected="True">Choose a font size</asp:ListItem>
<asp:ListItem Value="10">10pt</asp:ListItem>
<asp:ListItem Value="14">14pt</asp:ListItem>
<asp:ListItem Value="16">16pt</asp:ListItem>
</asp:DropDownList>
<br/>Border Style:
<asp:DropDownList id="drpBorderStyle" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpBorderStyle_SelectedIndexChanged">
<asp:ListItem Value="Choose a border style" Selected="True">Choose a border style</asp:ListItem>
<asp:ListItem Value="dashed">dashed</asp:ListItem>
<asp:ListItem Value="inset">inset</asp:ListItem>
<asp:ListItem Value="none">none</asp:ListItem>
<asp:ListItem Value="outset">outset</asp:ListItem>
<asp:ListItem Value="solid">solid</asp:ListItem>
</asp:DropDownList>
<br/>Border Width:
<asp:DropDownList id="drpBorderWidth" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpBorderWidth_SelectedIndexChanged">
<asp:ListItem Value="Select a border width" Selected="True">Select a border width</asp:ListItem>
<asp:ListItem Value="1">1 px</asp:ListItem>
<asp:ListItem Value="2">2 px</asp:ListItem>
<asp:ListItem Value="3">3 px</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
public partial class PropertyTester : System.Web.UI.Page
{
private Style _styleToApply = new Style();
protected void chkVisible_CheckedChanged(object sender, EventArgs e)
{
if (chkVisible.Checked)
ChangeVisible(true);
else
ChangeVisible(false);
}
protected void chkEnabled_CheckedChanged(object sender, EventArgs e)
{
if (chkEnabled.Checked)
ChangeEnabled(true);
else
ChangeEnabled(false);
}
protected void drpTextColor_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpTextColor.SelectedIndex > 0)
{
_styleToApply.ForeColor = Color.FromName(drpTextColor.SelectedValue);
ApplyNewStyle(_styleToApply);
}
}
protected void drpBackgroundColor_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpBackgroundColor.SelectedIndex > 0)
{
_styleToApply.BackColor = Color.FromName(drpBackgroundColor.SelectedValue);
ApplyNewStyle(_styleToApply);
}
}
protected void drpFont_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpFont.SelectedIndex > 0)
{
_styleToApply.Font.Name = drpFont.SelectedValue;
ApplyNewStyle(_styleToApply);
}
}
protected void drpFontSize_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpFontSize.SelectedIndex > 0)
{
int fontSize = Convert.ToInt32(drpFontSize.SelectedValue);
_styleToApply.Font.Size = FontUnit.Point(fontSize);
ApplyNewStyle(_styleToApply);
}
}
protected void drpBorderStyle_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpBorderStyle.SelectedIndex > 0)
{
string currBorderStyle = drpBorderStyle.SelectedValue;
if (currBorderStyle == "dashed")
_styleToApply.BorderStyle = BorderStyle.Dashed;
else if (currBorderStyle == "inset")
_styleToApply.BorderStyle = BorderStyle.Inset;
else if (currBorderStyle == "none")
_styleToApply.BorderStyle = BorderStyle.None;
else if (currBorderStyle == "outset")
_styleToApply.BorderStyle = BorderStyle.Outset;
else if (currBorderStyle == "solid")
_styleToApply.BorderStyle = BorderStyle.Solid;
ApplyNewStyle(_styleToApply);
}
}
protected void drpBorderWidth_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpBorderWidth.SelectedIndex > 0)
{
int borderSize = Convert.ToInt32(drpBorderWidth.SelectedValue);
_styleToApply.BorderWidth = Unit.Pixel(borderSize);
ApplyNewStyle(_styleToApply);
}
}
private void ApplyNewStyle(Style styleToSet)
{
labHere.ApplyStyle(styleToSet);
labAnother.ApplyStyle(styleToSet);
txtBox.ApplyStyle(styleToSet);
btnSample.ApplyStyle(styleToSet);
}
private void ChangeVisible(bool which)
{
labHere.Visible = which;
labAnother.Visible = which;
txtBox.Visible = which;
btnSample.Visible = which;
}
private void ChangeEnabled(bool which)
{
labHere.Enabled = which;
labAnother.Enabled = which;
txtBox.Enabled = which;
btnSample.Enabled = which;
}
}
Selection mode of asp:ListBox (C#)
<%@ Page Language="c#" Explicit="true" Strict="true" %>
<script language="c#" runat="Server">
public void Verify(object sender, EventArgs e)
{
if (CountryListBox.SelectedIndex != -1)
{
OutputLabel.Text = "You selected: ";
if (CountryListBox.SelectionMode == ListSelectionMode.Single)
OutputLabel.Text += "<b>" + CountryListBox.SelectedItem.Text + "</b>";
else
{
for (int j=0;j<CountryListBox.Items.Count;j++)
{
if (CountryListBox.Items[j].Selected)
OutputLabel.Text += "<b>" + CountryListBox.Items[j].Text + "</b>, ";
}
}
}
else
OutputLabel.Text = "You didn"t selected any country!";
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:ListBox ID="CountryListBox" Runat="server" SelectionMode="Multiple">
<asp:ListItem Value="1">United States</asp:ListItem>
<asp:ListItem Value="2">United Kingdom</asp:ListItem>
<asp:ListItem Value="3">China</asp:ListItem>
<asp:ListItem Value="4">India</asp:ListItem>
</asp:ListBox><br>
<asp:Button ID="SubmitButton" Runat="server" Text="Submit" OnClick="Verify"></asp:Button>
<asp:Label ID="OutputLabel" Runat="server"></asp:Label>
</form>
</body>
</html>
Selection mode of asp:ListBox (VB)
<%@ Page Language="vb" Explicit="true" Strict="true" %>
<script language="vb" runat="Server">
Public Sub Verify(sender As Object, e As EventArgs)
Dim lstItem As ListItem
If CountryListBox.SelectedIndex <> -1 Then
OutputLabel.Text = "You selected: "
If CountryListBox.SelectionMode = ListSelectionMode.Single Then
OutputLabel.Text += "<b>" & CountryListBox.SelectedItem.Text & "</b>"
Else
For Each lstItem In CountryListBox.Items
If lstItem.Selected Then
OutputLabel.Text += "<b>" & lstItem.Text & "</b>, "
End If
Next
End If
Else
OutputLabel.Text = "You didn"t selected any country!"
End If
End Sub
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:ListBox ID="CountryListBox" Runat="server" SelectionMode="Multiple">
<asp:ListItem Value="1">United States</asp:ListItem>
<asp:ListItem Value="2">United Kingdom</asp:ListItem>
<asp:ListItem Value="3">China</asp:ListItem>
<asp:ListItem Value="4">India</asp:ListItem>
</asp:ListBox><br>
<asp:Button ID="SubmitButton" Runat="server" Text="Submit" OnClick="Verify"></asp:Button>
<asp:Label ID="OutputLabel" Runat="server"></asp:Label>
</form>
</body>
</html>
Set array value to asp:ListBox DataSource (VB.net)
<script runat="server">
sub Page_Load(Sender as Object, e as EventArgs)
if not Page.IsPostBack then
dim arrColors() as string = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
lbColors.SelectedIndex = 0
lbColors.DataSource = arrColors
end if
DataBind()
end sub
</script>
<html><body>
<form runat="server">
<asp:Listbox runat="server" id="lbColors"
width="150"
AutoPostBack="true"
SelectionMode="Single" >
</asp:Listbox>
<asp:Label id="lblMessage" runat="server"
Text="<%# lbColors.SelectedItem.Text %>" />
</form>
</body></html>
Using the ListBox control (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(TextBox1.Text.ToString());
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "You selected from the ListBox:<br>";
foreach (ListItem li in ListBox1.Items) {
if (li.Selected == true) {
Label1.Text += li.Text + "<br>";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Using the ListBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
<asp:Button ID="Button1" Runat="server" Text="Add an additional item"
OnClick="Button1_Click" />
<br />
<br />
<asp:ListBox ID="ListBox1" Runat="server" SelectionMode="multiple">
<asp:ListItem>Hematite</asp:ListItem>
<asp:ListItem>Halite</asp:ListItem>
<asp:ListItem>Limonite</asp:ListItem>
<asp:ListItem>Magnetite</asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button2" Runat="server" Text="Submit"
OnClick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Using the ListBox control (VB)
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
ListBox1.Items.Add(TextBox1.Text.ToString())
End Sub
Protected Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = "You selected from the ListBox:<br>"
For Each li As ListItem In ListBox1.Items
If li.Selected = True Then
label1.Text += li.Text & "<br>"
End If
Next
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Using the ListBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
<asp:Button ID="Button1" Runat="server" Text="Add an additional item"
OnClick="Button1_Click" />
<br />
<br />
<asp:ListBox ID="ListBox1" Runat="server" SelectionMode="multiple">
<asp:ListItem>Hematite</asp:ListItem>
<asp:ListItem>Halite</asp:ListItem>
<asp:ListItem>Limonite</asp:ListItem>
<asp:ListItem>Magnetite</asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button2" Runat="server" Text="Submit"
OnClick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</div>
</form>
</body>
</html>