[Asp.net]Repeater 绑定 NameValueCollection 类型的数据源
2012-05-11 14:48
375 查看
今天在做一个功能的时候,需要把 Request.ServerVariables 属性绑定给 Repeater 控件显示,Request.ServerVariables 返回的是一个 NameValueCollection 对象,一个键值对的集合。
谷歌了一下,居然无一例外需要在 Repaeter_ItemDataBound 事件进行相应的处理,类似于下面的代码:
前台页面:
后台页面:
第一感觉,这样做太过繁琐,拉两个 Label 控件,还要在 Repeater_ItemDataBound 事件里面,编写代码。直接了当的方法不是很好, Eval 绑定语法不是就能搞定,在 Asp.net 页面就能搞定的事情,完全没有必要放到 cs 页面里去吧。
优化后的页面:
最后的话:
优化后的页面, 代码整洁了不少,也省下了不时间,心情也舒畅了不少,故而记录之。-,-
谷歌了一下,居然无一例外需要在 Repaeter_ItemDataBound 事件进行相应的处理,类似于下面的代码:
前台页面:
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"> <HeaderTemplate> <table class="no-style full"> <thead> <tr> <th>信息(键)</th> <th>信息(值)</th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="lblName" runat="server" Text=""></asp:Label> </td> <td> <asp:Label ID="lblValue" runat="server" Text=""></asp:Label> </td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater>
后台页面:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lblName = (Label)e.Item.FindControl("lblName"); Label lblValue = (Label)e.Item.FindControl("lblValue"); lblName.Text = e.Item.DataItem.ToString(); lblValue.Text = Request.ServerVariables[e.Item.DataItem.ToString()]; } }
第一感觉,这样做太过繁琐,拉两个 Label 控件,还要在 Repeater_ItemDataBound 事件里面,编写代码。直接了当的方法不是很好, Eval 绑定语法不是就能搞定,在 Asp.net 页面就能搞定的事情,完全没有必要放到 cs 页面里去吧。
优化后的页面:
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table class="no-style full"> <thead> <tr> <th>信息(键)</th> <th>信息(值)</th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Container.DataItem %> </td> <td> <%# Request.ServerVariables[Container.DataItem.ToString()] %> </td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater>
最后的话:
优化后的页面, 代码整洁了不少,也省下了不时间,心情也舒畅了不少,故而记录之。-,-
相关文章推荐
- 强类型ASP.NET数据绑定改进版
- ASP.NET数据绑定之Repeater控件
- ASP.NET Repeater 绑定 DropDownList Calendar 选择日期
- ASP.NET 2.0 中的几种数据源和数据绑定控件
- asp.net中Repeater嵌套绑定的方法(查询新闻类别和新闻类别下的新闻) .
- ASP.NET: Repeater绑定ArrayList数据源<Container.DataItem 是重点>
- ASP.Net绑定数据源
- asp.net Repeater 数据绑定代码
- ASP.NET实现TreeView的XML数据源绑定实例代码
- ASP.NET 手工绑定数据源时分页操作产生的问题
- Asp.net--数据绑定控件Repeater
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- asp.net中使用分页控件,并绑定数据到GridView或Repeater
- 如何应用Asp.Net Mvc内建功能(DefaultModelBinder)实现简单类型、复杂类型、集合类型,以及字典类型的自动绑定
- VS2013从头开始创建Web Form--ASP.NET数据源和数据绑定控件
- asp.net Repeater绑定时使用函数
- 理解asp.net中DropDownList编辑数据源,绑定数据库数据。
- ASP.NET 的数据绑定,DataList,Repeater 的绑定示例
- ASP.NET Repeater控件绑定List泛型的写法
- ASP.NET魔法---Repeater变量、对象单独绑定