ASP.Net/Asp Control/ListBox
Содержание
- 1 Add ListItem to ListBox
- 2 Allowing Multiple Selections in a ListBox Control (VB.net)
- 3 asp:listbox: On selected index changed
- 4 Change background color for asp:listbox (VB.net)
- 5 Get selected index from asp:listbox (VB.net)
- 6 Get selected index, text and value from asp:listbox (VB.net)
- 7 Get selected item from ListBox (C#)
- 8 Set static value for asp:listbox (VB.net)
- 9 Using the ListBox to select list items.
Add ListItem to ListBox
<form runat="server">
<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
if(!IsPostBack) {
ListItem item;
item = new ListItem("A");
languageListBox.Items.Add(item);
item = new ListItem("B");
languageListBox.Items.Add(item);
item = new ListItem("C");
languageListBox.Items.Add(item);
languageListBox.Rows = 3;
}
}
protected void ListBoxSelectionChanged(object o, EventArgs e) {
bool valueSet = false;
foreach(ListItem item in languageListBox.Items) {
if (item.Selected) {
if(valueSet) {
favoriteLanguage.Text += ", " + item.Text;
}
else {
favoriteLanguage.Text = item.Text;
valueSet = true;
}
}
}
}
</script>
<asp:ListBox
id="languageListBox"
runat="server"
SelectionMode="multiple"
OnSelectedIndexChanged="ListBoxSelectionChanged" /><br />
Favorite Language:
<b><asp:label runat="server"
id="favoriteLanguage"
style="color:blue"
Text="Not Set" /></b><br />
<asp:button runat="server" Text="Submit"/>
</form>
Allowing Multiple Selections in a ListBox Control (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Allowing Multiple Selections in a ListBox Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select a Color:<BR>
<BR><BR>
<asp:listbox id="lb1" runat="server" selectionmode="multiple" rows=4>
<asp:listitem value="Bu">Blue</asp:listitem>
<asp:listitem value="Re">Red</asp:listitem>
<asp:listitem value="Gr">Green</asp:listitem>
<asp:listitem value="Pu" Selected>Purple</asp:listitem>
<asp:listitem value="Ba">Black</asp:listitem>
<asp:listitem value="Go" text="Gold"/>
</asp:listbox>
</form>
</BODY>
</HTML>
asp:listbox: On selected index changed
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub lb1_Changed(Sender As Object, E As EventArgs)
lb1.BackColor = System.Drawing.Color.FromName(lb1.SelectedItem.Text)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires When the Selection Changes in a ListBox Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select a Color:<BR>
<BR><BR>
<asp:listbox id="lb1" runat="server" autopostback="True" onselectedindexchanged="lb1_Changed" rows=3>
<asp:listitem value="Bu">Blue</asp:listitem>
<asp:listitem value="Re">Red</asp:listitem>
<asp:listitem value="Gr">Green</asp:listitem>
<asp:listitem value="Pu" >Purple</asp:listitem>
<asp:listitem value="Ba">Black</asp:listitem>
</asp:listbox>
</form>
</BODY>
</HTML>
Change background color for asp:listbox (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub lb1_Changed(Sender As Object, E As EventArgs)
lb1.BackColor = System.Drawing.Color.FromName(lb1.SelectedItem.Text)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires When the Selection Changes in a ListBox Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select a Color:<BR>
<BR><BR>
<asp:listbox id="lb1" runat="server" autopostback="True" onselectedindexchanged="lb1_Changed" rows=3>
<asp:listitem value="Bu">Blue</asp:listitem>
<asp:listitem value="Re">Red</asp:listitem>
<asp:listitem value="Gr">Green</asp:listitem>
<asp:listitem value="Pu" >Purple</asp:listitem>
<asp:listitem value="Ba">Black</asp:listitem>
</asp:listbox>
</form>
</BODY>
</HTML>
Get selected index from asp:listbox (VB.net)
<script runat="server" language="vb">
Sub Page_Load()
Dim msg As String = "You have selected: <br />"
If list1.Items(0).Selected Then msg = msg & list1.Items(0).Text & "<br />"
If list1.Items(1).Selected Then msg = msg & list1.Items(1).Text & "<br />"
If list1.Items(2).Selected Then msg = msg & list1.Items(2).Text & "<br />"
Message.Text = msg
End Sub</script>
<html>
<head>
<title>Drop Down List Example</title>
</head>
<body>
<asp:label id="Message" runat="server"/>
<br />
<form runat="server">
Which city do you wish to look at hotels for?<br /><br />
<asp:listbox id="list1" runat="server" selectionmode="multiple">
<asp:listitem>Madrid</asp:listitem>
<asp:listitem>Oslo</asp:listitem>
<asp:listitem>Lisbon</asp:listitem>
</asp:listbox>
<br /><br /><br /><br />
<input type="Submit">
</form>
</body>
</html>
Get selected index, text and value from asp:listbox (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblDataSelected.Text = "Selected Text: " _
& lb1.SelectedItem.Text & "<BR>Selected Value: " _
& lb1.SelectedItem.Value & "<BR>Selected Index: " _
& lb1.SelectedIndex
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic ListBox Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:label id="lblDataSelected" runat="server"/>
<BR><BR>
<asp:listbox id="lb1" runat="server" rows=3>
<asp:listitem value="Bu">Blue</asp:listitem>
<asp:listitem value="Re">Red</asp:listitem>
<asp:listitem value="Gr">Green</asp:listitem>
<asp:listitem value="Pu" Selected>Purple</asp:listitem>
<asp:listitem value="Ba">Black</asp:listitem>
<asp:listitem value="Go" text="Gold"/>
</asp:listbox>
<BR><BR>
<asp:button id="butOK" text="OK" type="Submit" onclick="SubmitBtn_Click" runat="server"/>
</form>
</BODY>
</HTML>
Get selected item from ListBox (C#)
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
string msgCitiesList = "";
if (Page.IsPostBack == true)
if (list1.Items[0].Selected == true)
{
msgCitiesList = msgCitiesList + list1.Items[0].Text + "<br />";
}
if (list1.Items[1].Selected)
{
msgCitiesList = msgCitiesList + list1.Items[1].Text + "<br/>";
}
if (list1.Items[2].Selected)
{
msgCitiesList = msgCitiesList + list1.Items[2].Text + "<br />";
}
if (msgCitiesList != "")
{
Message.Text = "You have selected: <br />" + msgCitiesList;
}
else
{
Message.Text = "";
}
}
</script>
<html>
<head>
<title>List Box Example</title>
</head>
<body>
<asp:Label id="Message" runat="server"></asp:Label>
<br />
Which city do you wish to look at hotels for?<br />
<form runat="server">
<asp:listbox id="list1" runat="server" selectionmode="multiple">
<asp:listitem>A</asp:listitem>
<asp:listitem>B</asp:listitem>
<asp:listitem>C</asp:listitem>
</asp:listbox>
<br />
<input type="submit" value="Submit Query" />
</form>
</body>
</html>
Set static value for asp:listbox (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic ListBox Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select a Color:<BR>
<BR><BR>
<asp:listbox id="lb1" runat="server" rows=3>
<asp:listitem value="Bu">Blue</asp:listitem>
<asp:listitem value="Re">Red</asp:listitem>
<asp:listitem value="Gr">Green</asp:listitem>
<asp:listitem value="Pu" Selected>Purple</asp:listitem>
<asp:listitem value="Ba">Black</asp:listitem>
<asp:listitem value="Go" text="Gold"/>
</asp:listbox>
</form>
</BODY>
</HTML>
Using the ListBox to select list items.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
protected void btnAdd_Click(object sender, EventArgs e)
{
ListItem item = lstAllProducts.SelectedItem;
if (item != null)
{
lstAllProducts.Items.Remove(item);
lstFavoriteProducts.ClearSelection();
lstFavoriteProducts.Items.Add(item);
}
}
protected void btnRemove_Click(object sender, EventArgs e)
{
ListItem item = lstFavoriteProducts.SelectedItem;
if (item != null)
{
lstFavoriteProducts.Items.Remove(item);
lstAllProducts.ClearSelection();
lstAllProducts.Items.Add(item);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (ListItem item in lstFavoriteProducts.Items)
lblResults.Text += "<li>" + item.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" runat="server">
<asp:ListBox
id="lstAllProducts"
DataSourceID="srcProducts"
DataTextField="Title"
DataValueField="Id"
Runat="server" />
<asp:Button
id="btnAdd"
Text=">"
ToolTip="Add List Item"
Runat="server"
OnClick="btnAdd_Click" />
<br />
<asp:Button
id="btnRemove"
Text="<"
ToolTip="Remove List Item"
Runat="server"
OnClick="btnRemove_Click" />
</div>
<asp:ListBox
id="lstFavoriteProducts"
Runat="server" />
</div>
<asp:Button
id="btnSubmit"
Text="Submit Form"
Runat="server" OnClick="btnSubmit_Click" />
</p>
<asp:Label
id="lblResults"
EnableViewState="false"
Runat="server" />
<asp:SqlDataSource
id="srcProducts"
SelectCommand="SELECT Id, Title FROM Products"
ConnectionString="<%$ ConnectionStrings:Products %>"
Runat="server" />
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="Products"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
</configuration>