ASP.NET Tutorial/Data Binding/BulletedList — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:58, 26 мая 2010

BulletedList Control data binding with asp:SqlDataSource

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <title>Show BulletedList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:BulletedList
        id="blProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </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>


BulletStyle property

Circle
CustomImage
Disc
LowerAlpha
LowerRoman
NotSet
Numbered
Square
UpperAlpha
UpperRoman

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <title>Show BulletedList Image</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:BulletedList
        id="blProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        BulletStyle="CustomImage"
        BulletImageUrl="~/Images/Bullet.gif"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </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>


DisplayMode property: BulletedList DisplayMode enumeration

HyperLink:      Each list item is rendered as a link to another page.
LinkButton:     Each list item is rendered by a LinkButton control.
Text:           Each list item is rendered as plain text.
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <title>Show BulletedList HyperLinks</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:BulletedList
        id="blWebsites"
        DisplayMode="HyperLink"
        Target="_blank"
        Runat="server">
        <asp:ListItem
            Text="Yahoo"
            Value="http://www.Yahoo.ru" />
        <asp:ListItem
            Text="Google"
            Value="http://www.Google.ru" />
        <asp:ListItem
            Text="asdf"
            Value="http://www.asdfasdfasdf.ru" />
    </asp:BulletedList>
    </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>


Page with declarative data binding

<%@ Page Language="C#" Debug="true" %>
<!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>
    <title>Simple page with declarative data binding</title>
</head>
<body>
    <form runat="server" id="_form">
        
        
        <asp:BulletedList runat="server" ID="_displayItems" 
                          DataSourceID="_itemsDataSource">
            <asp:ListItem>Sample item 1</asp:ListItem>
            <asp:ListItem>Sample item 2 ...</asp:ListItem>
        </asp:BulletedList>
        
        <h2 runat="server" id="_messageH2">Total number of items = xx</h2>
        
        <asp:ObjectDataSource runat="server" ID="_itemsDataSource"
             TypeName="Architecture.MyDataSource"
             SelectMethod="GetItems" />
    </form>
</body>
</html>

File: MyDataSource.cs
using System;
namespace Architecture
{
  public static class MyDataSource
  {
    static string[] _items = 
      {"Item #1", "Item #2", "Item #3", "Item #4", 
       "Item #5", "Item #6", "Item #7", "Item #8", 
       "Item #9", "Item #10"};
    public static string[] GetItems()
    {
      return _items;
    }
  }
}


Simple Page With Data Binding

<%@ Page Language="C#" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">    
string[] _displayItemData = {"Item #1", "Item #2", "Item #3", "Item #4", "Item #5",
    "Item #6", "Item #7", "Item #8", "Item #9", "Item #10"};
protected override void OnLoad(EventArgs e)
{
    _messageH2.InnerText = "Total number of items = " + _displayItemData.Length.ToString();
   _displayItems.DataSource = _displayItemData;
   _displayItems.DataBind();
          
    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 data binding</h1>
        
        <asp:BulletedList runat="server" ID="_displayItems">
            <asp:ListItem>Sample item 1</asp:ListItem>
            <asp:ListItem>Sample item 2 ...</asp:ListItem>
        </asp:BulletedList>
        
        <h2 runat="server" id="_messageH2">Total number of items = xx</h2>
    </form>
</body>
</html>