Hopes

Start Here..

 

Page.DataBind()方法

我们先来看一个简单的例子:

C#代码  收藏代码
  1. <% @ Page Language="C#" %>  
  2. <% @ Import Namespace="System.Data" %>  
  3. <Script Language="C#" Runat="Server">  
  4. public void Page_Load(Object src,EventArgs e)  
  5. {  
  6.    //首先建立一个数组  
  7.    ArrayList arr=new ArrayList();   
  8.    arr.Add("飞刀");  
  9.    arr.Add("Zsir");  
  10.    arr.Add("大风");  
  11.    arr.Add("布丁");  
  12.    arr.Add("亚豪");  
  13.   
  14.    //将数组捆绑到DropDownList控件上去  
  15.    DDL.DataSource = arr;  
  16.    DDL.DataBind();  
  17. }  
  18. </script>  
  19. <html>  
  20. <head>  
  21. <title></title>  
  22. </head>  
  23. <body>  
  24. <asp:DropDownList id="DDL" runat="server" />  
  25. </body>  
  26. </html>  

 

我们在代码中可以看到我们建立了一个DropDownList,但是他没有<asp:ListItem>属性,而我们从最后的显示中依然可以看到我们所列出的选项。

这里就是我们用DataBind的结果,在Page_Load方法中我们建立了一个数组(ArrayList),并通过DataBind方法将这个数组捆绑到了DropDownList控件中,使得DropDownList最后有数据显示:),怎么样对Bind有一定感性认识了吧。下面我们开始正式讲解

其实DataBind(),不仅能对控件进行捆绑,而且还能够对页面中属性,方法进行捆绑,甚至整个页面都可以捆绑。比如,调用Page.DataBind()方法或者直接使用DataBind(),那么整个页面都将被捆绑,所有的数据全在监视之下。下面的例子,我们将使用DataBind方法来捆绑DropDownList,并获得其中的数据

  收藏代码
  1. <% @ Page Language="C#" %>  
  2. <% @ Import Namespace="System.Data" %>  
  3. <Script Language="C#" Runat="Server">  
  4. public void sub_Click(Object sender,EventArgs e)  
  5. {  
  6. Page.DataBind();  
  7. }  
  8. </script>  
  9. <html>  
  10. <head>  
  11. <title></title>  
  12. </head>  
  13. <body>  
  14. <form runat=server>  
  15.   <asp:DropDownList id="DDL" runat="server" >  
  16.   <asp:ListItem>ASP技术</asp:ListItem>  
  17.   <asp:ListItem selected>ASP.Net技术</asp:ListItem>  
  18.   <asp:ListItem>JSP技术</asp:ListItem>  
  19.   <asp:ListItem>PHP技术</asp:ListItem>  
  20.   <asp:ListItem>组件技术</asp:ListItem>  
  21.   </asp:DropDownList>  
  22. <br>  
  23. 你现在选择的是:<font color=red><%# DDL.SelectedItem.Text %></font>区  
  24. <br>  
  25. <asp:Button id="sub" Text="提交" Type="submit" runat=server OnClick="sub_Click" />  
  26. </form>  
  27. </body>  
  28. </html>  

 

我们看到,那个红色的[JSP技术],我们并没有使用什么控件,但是他却能正确的显示我们的选择结果,这个是就捆绑的结果,注意<%# DDL.SelectedItem.Text %>这句话,正是它让我们取得了捆绑的数据。它看上去是不是像我们熟悉的<%=...%>这个语句,它们的使用方法相差不多,只是<%=...%>是在程序执行时调用,<%#... %>是在DataBind()方法之后被调用。以后我们还能经常看到他的身影,呵呵。

接收Bind的控件,一般有DropDownList,DataList,DataGrid,ListBox这些集合性质的控件,而被捆绑的主要是ArrayList(数组),Hashtable(哈稀表),DataView(数据视图),DataReader这四个,以后我们就可以对号入座,不会出现DataTable被捆绑的错误了:)

讲到Bind,就不能说一下DataBinder.Eval()方法.

我们在使用DataBind,获得的数据,系统会将其默认为String(字符串),这对我们平时的输出显示提供了极大的方便,但是我们并不是每次都需要string类型,有时我们就需要Boolean,Int32这些类型。此时我们就需要转化类型了。可能大家最先想到的是String.Format方法,这是最好的,但是用法过于烦。所以最好不要这样啦。我们可以用DataBinder.Eval()方法,他的格式是:

DataBinder.Eval(Container.DataItem,"转换的类型","格式")

最后一个"格式"是可选的,一般不用去管他,Container.DataItem是捆绑的数据项,"转换类型"指的是Integer,String,Boolean这一类东西.

有了它,我们处理数据就更加方便

最后我们来看一个例子,关于DataView的捆绑,呵呵,老是用DropDownList烦了,这次用DataGrid,在这个例子中我们可以看到"表是怎样炼成的"

C#代码  收藏代码
  1. <% @ Page Language="C#" %>  
  2. <% @ Import Namespace="System.Data" %>  
  3. <Script Language="C#" Runat="Server">  
  4. public void Page_Load(Object src,EventArgs e)  
  5. {  
  6. int i;  
  7. //建立表数据  
  8. DataTable dt=new DataTable();  
  9. DataRow dr;  
  10. //建立Column例,可以指明例的类型,这里用的是默认的string  
  11. dt.Columns.Add(new DataColumn("编号"));  
  12. dt.Columns.Add(new DataColumn("用户名"));  
  13. for(i=1;i<8;i++)  
  14. {  
  15. dr=dt.NewRow();   
  16. dr[0]=Int32.ToString(i);  
  17. dr[1]="aspcn"+Int32.ToString(i);  
  18. dt.Rows.Add(dr);  
  19. }  
  20. //捆绑  
  21. DG1.DataSource = new DataView(dt);  
  22. DG1.DataBind();  
  23.   
  24.   
  25. }  
  26. </script>  
  27. <html>  
  28. <head>  
  29. <title></title>  
  30. </head>  
  31. <body>  
  32. <asp:DataGrid id="DG1" runat=server align=center HeaderStyle-BackColor="#aaaadd" />  
  33.   
  34. </body>  
  35. </html>  

posted on 2012-05-10 20:20 ** 阅读(613) 评论(1)  编辑  收藏

评论

# re: Page.DataBind()方法 2013-12-09 11:34 乖乖

成功后吃饭  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

你好!

常用链接

留言簿(2)

随笔档案

文章分类

文章档案

新闻档案

相册

收藏夹

C#学习

友情链接

搜索

最新评论

阅读排行榜

评论排行榜