posts - 7,  comments - 3,  trackbacks - 0

ASP.NET 引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。下表显示了新语法的一些示例。

 

简单属性 Customer: <%# custID %>
集合 Orders: <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server">
表达式 Contact: <%# ( customer.First Name + " " + customer.LastName ) %>
方法结果 Outstanding Balance: <%# GetBalance(custID) %>

1. 绑定到属性

C#:

 1void Page_Load(Object sender, EventArgs e)
 2{
 3  Page.DataBind();
 4}

 5        
 6string custID
 7{
 8  get {return "ALFKI";}
 9}

10        
11int orderCount
12{
13  get {return 11;}
14}

 

ASP:

 

1<body>
2  <h3><font face="宋体">到页属性的数据绑定</font></h3>
3  <form runat=server>
4     客户:<b><%# custID %></b><br>
5     未结的订单:<b><%# orderCount %></b>
6  </form>
7</body>


2. 绑定到集合和列表

绑定到Array List

C# :

 

 1void Page_Load(Object Sender, EventArgs E)
 2{
 3  if (!Page.IsPostBack)
 4  {
 5    ArrayList values = new ArrayList();
 6       values.Add ("IN");
 7       values.Add ("KS");
 8       values.Add ("MD");
 9       values.Add ("MI");
10       values.Add ("OR");
11       values.Add ("TN");
12       DropDown1.DataSource = values;
13       DropDown1.DataBind();
14    }

15}

16
17void SubmitBtn_Click(Object sender, EventArgs e)
18{
19   Label1.Text = "您选择了:" + DropDown1.SelectedItem.Text;
20}

21


ASP:

 

 1<body>
 2  <h3><font face="宋体">数据绑定 DropDownList</font></h3>
 3  <form runat=server>
 4    <asp:DropDownList id="DropDown1" runat="server" />
 5    <asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>
 6    <p>
 7    <asp:Label id=Label1 font-name="宋体" font-size="10.5pt" runat="server" />
 8  </form>
 9</body>
10



绑定到Hash Table

C#:

 

 1        void Page_Load(Object sender, EventArgs e) {
 2            if (!Page.IsPostBack) {
 3
 4                Hashtable h = new Hashtable();
 5                h.Add ("键 1""值 1");
 6                h.Add ("键 2""值 2");
 7                h.Add ("键 3""值 3");
 8
 9                MyDataList.DataSource = h;
10                MyDataList.DataBind();
11            }

12        }



ASP:

 

 1<body>
 2
 3    <h3><font face="宋体">到哈希表的数据绑定</font></h3>
 4
 5    <form runat=server>
 6
 7        <asp:DataList id="MyDataList" runat="server"
 8          BorderColor="black"
 9          BorderWidth="1"
10          GridLines="Both"
11          CellPadding="4"
12          CellSpacing="0"
13          >
14
15            <ItemTemplate>
16                <%# ((DictionaryEntry)Container.DataItem).Key %> :
17                <%# ((DictionaryEntry)Container.DataItem).Value %>
18            </ItemTemplate>
19
20        </asp:DataList>
21
22    </form>
23
24</body>

 

3. 绑定到方法或表达式

 绑定时的表达式:<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>

C#:

 

 1       void Page_Load(Object Src, EventArgs E) {
 2
 3            if (!Page.IsPostBack) {
 4
 5               ArrayList values = new ArrayList();
 6
 7               values.Add (0);
 8               values.Add (1);
 9               values.Add (2);
10               values.Add (3);
11               values.Add (4);
12               values.Add (5);
13               values.Add (6);
14
15               DataList1.DataSource = values;
16               DataList1.DataBind();
17            }

18        }

19
20        String EvenOrOdd(int number) {
21            if ((number % 2== 0)
22              return "偶数";
23            else
24              return "奇数";
25        }

26

 

ASP:

 

 1<body>
 2
 3    <h3><font face="宋体">到方法和表达式的数据绑定</font></h3>
 4
 5    <form runat=server>
 6
 7      <asp:DataList id="DataList1" runat="server"
 8        BorderColor="black"
 9        BorderWidth="1"
10        GridLines="Both"
11        CellPadding="3"
12        CellSpacing="0"
13        >
14
15        <ItemTemplate>
16          数字值:<%# Container.DataItem %>
17          偶/奇:<%# EvenOrOdd((int) Container.DataItem) %>
18        </ItemTemplate>
19
20      </asp:datalist>
21
22    </form>
23
24</body>
25
26
posted on 2008-07-13 10:29 reddyone 阅读(530) 评论(0)  编辑 收藏 引用 所属分类: .net
只有注册用户登录后才能发表评论。