ASP.NET Tutorial/ASP.net Controls/BulletedList

Материал из .Net Framework эксперт
Версия от 12:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add ListItem to BulletedList

<%@ Page Language="C#" Debug="true" Trace="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
const int _itemCount = 10;
        
string GetDisplayItem(int n)
{
    return "Item #" + n.ToString();
}
protected override void OnLoad(EventArgs e)
{
    _displayList.Items.Clear();
    
    for (int i=0; i<_itemCount; i++)
        _displayList.Items.Add(new ListItem(GetDisplayItem(i)));
    
    _messageH2.InnerText = "Total number of items = " + _itemCount.ToString();
    
    base.OnLoad(e);
}    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Simple page with controls</title>
</head>
<body>
    <form runat="server" id="_form" >
    <h1>Test ASP.NET 2.0 Page with controls</h1>
    <asp:BulletedList runat="server" ID="_displayList">
        <asp:ListItem>Sample Item 1</asp:ListItem>
        <asp:ListItem>Sample Item 2</asp:ListItem>
        <asp:ListItem>Sample Item 3</asp:ListItem>
    </asp:BulletedList>
    <h2 runat="server" id="_messageH2">Total number of items = xx</h2>
    </form>
</body>
</html>


Add value to asp:BulletedList in a for loop (VB.net)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Sub Page_Load()
        For i As Integer = 1 To 100
            bltList.Items.Add("Item " & i.ToString())
        Next
    End Sub
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <style type="text/css">
        html
        {
            background-color:silver;
        }
        .contents
        {
            background-color:white;
            width:200px;
            height:200px;
        }
    </style>
    <title>Panel ScrollBars</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Panel
        id="pnlContent"
        ScrollBars="Auto"
        CssClass="contents"
        Runat="server">
        <asp:BulletedList
            id="bltList"
            Runat="server" />
    </asp:Panel>    
    
    </div>
    </form>
</body>
</html>


BulletedList with DynamicImage

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="BulletedListDynamicImageTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>BulletedList Dynamic Image Test</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Simple Bulleted List</h2>
            Select a bulleted list style
            <asp:DropDownList ID="drpStyles" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drpStyles_SelectedIndexChanged">
                <asp:ListItem>Circle</asp:ListItem>
                <asp:ListItem>Disc</asp:ListItem>
                <asp:ListItem>LowerAlpha</asp:ListItem>
                <asp:ListItem>LowerRoman</asp:ListItem>
                <asp:ListItem>Numbered</asp:ListItem>
                <asp:ListItem>Square</asp:ListItem>
                <asp:ListItem>UpperAlpha</asp:ListItem>
                <asp:ListItem>UpperRoman</asp:ListItem>
                <asp:ListItem>CustomImage</asp:ListItem>
            </asp:DropDownList>
            <asp:BulletedList ID="blItems" runat="server">
                <asp:ListItem>Item 1</asp:ListItem>
                <asp:ListItem>Item 2</asp:ListItem>
                <asp:ListItem>Item 3</asp:ListItem>
            </asp:BulletedList>
        </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;
public partial class BulletedListDynamicImageTest : System.Web.UI.Page
{
  protected void drpStyles_SelectedIndexChanged(object sender, EventArgs e)
  {
    string sBullet = drpStyles.SelectedItem.Text;
    BulletStyle bs = (BulletStyle)Enum.Parse(typeof(BulletStyle), sBullet, true);
    blItems.BulletStyle = bs;
    if (bs == BulletStyle.CustomImage){
      blItems.BulletImageUrl = "images/bulletPlus.gif";
      }
  }
}


DisplayMode of BulletedList

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="BulletedListTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>BulletedList Test</title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="container"> 
        <h1>BulletedList Demo</h1>
        Choose a display mode:
        <asp:DropDownList ID="drpDisplayMode" 
                          runat="server" 
                          OnSelectedIndexChanged="drpDisplayMode_SelectedIndexChanged" 
                          AutoPostBack="True">
          <asp:ListItem Selected="True">Text</asp:ListItem>
          <asp:ListItem>HyperLink</asp:ListItem>
          <asp:ListItem>LinkButton</asp:ListItem>
        </asp:DropDownList>
            <asp:BulletedList ID="blUrls" runat="server" DisplayMode="Text" 
                    BulletStyle="Square" OnClick="blItems_Click">
                <asp:ListItem Value="http://www.microsoft.ru">Microsoft</asp:ListItem>
                <asp:ListItem Value="http://www.adobe.ru">Adobe</asp:ListItem>
                <asp:ListItem Value="http://www.oracle.ru">Oracle</asp:ListItem>
            </asp:BulletedList>
            <asp:Label ID="labMsg" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Drawing;
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;
public partial class BulletedListTest : System.Web.UI.Page
{
  protected void blItems_Click(object sender, BulletedListEventArgs e)
  {
    labMsg.Text = "The bullet item index you selected was " + e.Index;
  }
  protected void drpDisplayMode_SelectedIndexChanged(object sender, EventArgs e)
  {
    string sMode = drpDisplayMode.SelectedItem.Text;
    BulletedListDisplayMode mode;
    mode  = (BulletedListDisplayMode)Enum.Parse(typeof(BulletedListDisplayMode), sMode, true);
    blUrls.DisplayMode = mode;
  }
}


Dynamically populating a BulletedList server control

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>BulletedList Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:BulletedList ID="BulletedList1" 
                         Runat="server" 
                         DataSourceID="XmlDataSource1" 
                         DataTextField="Title">
       </asp:BulletedList>
       <asp:XmlDataSource ID="XmlDataSource1" 
                          Runat="server" 
                          DataFile="Data.xml" 
                          XPath="FilmChoices/Film">
       </asp:XmlDataSource>
   </form>
</body>
</html>
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<FilmChoices>
  <Film
    Title="title 1"
    Year="1977"
    Director="A" />
  <Film
    Title="title 2"
    Year="1978"
    Director="B" />
  <Film
    Title="Title 3"
    Year="1962"
    Director="D" />
</FilmChoices>


Fill all asp:BulletedList style constant to asp:BulletedList, and click action (C#)

File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="BulletedList_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
    <div>
        Bullet styles:<br />
        <br />
        <asp:BulletedList 
             BulletStyle="Numbered" 
             DisplayMode="LinkButton" 
             ID="BulletedList1"
             OnClick="BulletedList1_Click" 
             runat="server">
        </asp:BulletedList>   
    </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;
public partial class BulletedList_aspx : System.Web.UI.Page
{
     
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
      foreach (string style in
        Enum.GetNames(typeof(BulletStyle)))
      {
        BulletedList1.Items.Add(style);
      }
    }
    }
  protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
  {
    string styleName = BulletedList1.Items[e.Index].Text;
    BulletStyle style = (BulletStyle)Enum.Parse(typeof(BulletStyle), styleName);
    BulletedList1.BulletStyle = style;
  }
}