BulletedList控件基本
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void BulletedList1_Click(object sender, System.Web.UI.WebControls.BulletedListEventArgs e)
{
Label1.Text = "The index of item you selected: " + e.Index + "<br>The value of the item selected: " + BulletedList1.Items[e.Index].Text;
}
private List<String> groceries = new List<String>();
void Page_Load()
{
groceries.Add("milk");
groceries.Add("steak");
groceries.Add("fish");
}
protected void Sort_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Sort")
{
switch (e.CommandArgument.ToString())
{
case "ASC":
groceries.Sort(SortASC);
break;
case "DESC":
groceries.Sort(SortDESC);
break;
}
}
}
void Page_PreRender()
{
bltGroceries.DataSource = groceries;
bltGroceries.DataBind();
}
int SortASC(string x, string y)
{
return String.Compare(x, y);
}
int SortDESC(string x, string y)
{
return String.Compare(x, y) * -1;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bltResults.DataSource = Calendar1.SelectedDates;
bltResults.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>BulletedList的OnClick事件:</h4>
<asp:BulletedList ID="BulletedList1" Runat="server" OnClick="BulletedList1_Click" DisplayMode="LinkButton">
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem>United Kingdom</asp:ListItem>
<asp:ListItem>Finland</asp:ListItem>
<asp:ListItem>Russia</asp:ListItem>
<asp:ListItem>Sweden</asp:ListItem>
<asp:ListItem>Estonia</asp:ListItem>
</asp:BulletedList>
<asp:Label ID="Label1" Runat="server"></asp:Label>
<hr />
<h4>BulletedList数据源为FilmChoices.xml</h4>
<asp:BulletedList ID="BulletedList2" Runat="server" DataSourceID="XmlDataSource1" DataTextField="Title"></asp:BulletedList>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="~/FilmChoices.xml" XPath="FilmChoices/Film"></asp:XmlDataSource>
<hr />
<asp:Button
ID="btnSortASC"
runat="server"
Text="Sort ASC"
CommandName="Sort"
CommandArgument="ASC"
OnCommand="Sort_Command" />
<asp:Button
ID="btnSortDESC"
runat="server"
Text="Sort DESC"
CommandName="Sort"
CommandArgument="DESC"
OnCommand="Sort_Command"/>
<br />
<asp:BulletedList ID="bltGroceries" runat="server"/>
<hr />
CommandArgument属性:取得或設定選擇性 (Optional) 參數,與相關的 CommandName 一起傳遞至 Command 事件。
<br />
<br />
<br />
<h4>bulletedlist数据源为日期:</h4>
<br />
<div>
<asp:Calendar
id="Calendar1"
SelectionMode="DayWeekMonth"
Runat="server"
/>
<br />
<asp:Button
id="btnSubmit"
text="Submit"
OnClick="btnSubmit_Click"
Runat="server"
/>
<hr />
<asp:BulletedList id="bltResults" DataTextFormatString="{0:d}"
Runat="server"
/>
</div>
</div>
</form>
</body>
</html>