Repeater中OnItemDataBound使用方式
来源:互联网 发布:网络舆论论文 编辑:程序博客网 时间:2024/06/01 07:32
今天遇到一个问题,在Repeater中绑定数据时候,还需要在Repeater中的添加子项目信息。
直接贴图:
在绑定题目的时候,需要将题目对应的问题选项给显示出来,这些数据来自不同的表中。属于一对多的关系。
<asp:Repeater runat="server" ID="EQInfo" OnItemDataBound="EQInfo_ItemDataBound" > <ItemTemplate> <tr> <td id="TableBodySelectColumn" runat="server"> <input type="radio" name="rad" value="<%#Eval("TID") %>" /></td> <td><%#Eval("TopicName") %></td> <td><%#OAS.Common.EnumDictionary.GetEnumDescription((Common.EnumDictionary.TopicType)Enum.Parse(typeof(Common.EnumDictionary.TopicType), Eval("TopicType").ToString()))%></td> <td class="td-has-select"> <a tabindex="0" runat="server" id="atabindex" class="btn btn-xs options-show" role="button" data-container="body" data-toggle="popover" data-trigger="focus" title="选项:" data-placement="left" data-html="true" data-content="">点击查看选项</a> </td> <td id="TableBodyOperationColumn" runat="server"> <%-- <span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="UpItem(this);"></span> <span class="glyphicon glyphicon-arrow-down" aria-hidden="true" onclick="DownItem(this);"></span>--%> <asp:LinkButton class="glyphicon glyphicon-arrow-up" runat="server" aria-hidden="true" ID="UpItem" CommandName='<%#Eval("TID")%>' OnClick="UpItem_Click"></asp:LinkButton> <asp:LinkButton class="glyphicon glyphicon-arrow-down" runat="server" aria-hidden="true" ID="DownItem" CommandName='<%#Eval("TID")%>' OnClick="DownItem_Click"></asp:LinkButton> </td> </tr> </ItemTemplate> </asp:Repeater>
protected void EQInfo_ItemDataBound(object sender, RepeaterItemEventArgs e) { try { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { HtmlAnchor atabindex = e.Item.FindControl("atabindex") as HtmlAnchor; A topic = e.Item.DataItem as A; List<A> options = new A().SelectBy(new A { TID = topic.TID }); string strData = ""; foreach (A item in options) { strData += item.OptionName + "</br>"; } Label lblPID = e.Item.FindControl("lblPID") as Label;//如果是绑定的DataReader则找不到,不知何故。 lblPID.Text = drv["PID"].ToString(); TextBox txtPName = e.Item.FindControl("txtPName") as TextBox; txtPName.Text = drv["pName"].ToString(); atabindex.Attributes.Add("data-content", strData.TrimEnd()); } } catch (Exception) { throw; } }
在RepeaterItemEventArgs中有绑定行的数据,需要对数据进行强制转化,通过e取获取需要绑定的控件,每个前台标签都对应一个html控件。并且可以对该控件进行一系列设置。
以下是微软给出来的方案:
<%@ Page Language="C#" AutoEventWireup="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>OnItemDataBound Example</title><script language="C#" runat="server"> void Page_Load(Object Sender, EventArgs e) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add(new Evaluation("Razor Wiper Blades", "Good")); values.Add(new Evaluation("Shoe-So-Soft Softening Polish", "Poor")); values.Add(new Evaluation("DynaSmile Dental Fixative", "Fair")); Repeater1.DataSource = values; Repeater1.DataBind(); } } void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { // This event is raised for the header, the footer, separators, and items. // Execute the following logic for Items and Alternating Items. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (((Evaluation)e.Item.DataItem).Rating == "Good") { ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>"; } } } public class Evaluation { private string productid; private string rating; public Evaluation(string productid, string rating) { this.productid = productid; this.rating = rating; } public string ProductID { get { return productid; } } public string Rating { get { return rating; } } } </script> </head> <body> <h3>OnItemDataBound Example</h3> <form id="form1" runat="server"> <br /> <asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server"> <HeaderTemplate> <table border="1"> <tr> <td><b>Product</b></td> <td><b>Consumer Rating</b></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' Runat="server"/> </td> <td> <asp:Label id="RatingLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <br /> </form> </body> </html>
阅读全文
0 0
- Repeater中OnItemDataBound使用方式
- 使用Repeater中的OnItemCommand,OnItemDataBound
- Repeater OnItemDataBound事件
- GridView的RowDataBound和Repeater的OnItemDataBound
- Repeater中嵌套使用Repeater
- Repeater中使用Button
- 在Repeater中嵌套使用Repeater
- 在Repeater中嵌套使用Repeater
- 在Repeater中嵌套使用Repeater
- 在Repeater中嵌套使用Repeater
- 在Repeater中嵌套使用Repeater
- 在Repeater中嵌套使用Repeater
- DataGrid中嵌套使用Repeater
- DataGrid中嵌套使用Repeater
- DataGrid中嵌套使用Repeater
- 在 repeater 中使用 Radiobutton
- .net中repeater控件使用
- ASP.NET 2.0 之在Repeater中嵌套使用Repeater
- Unity3d设计模式之单例模式
- Datagrid里面如何捕获前一页下一页最后页最前页的事件?
- 深入理解JavaScript系列(44):设计模式之桥接模式
- Openssh移植到Android系统的步骤
- CFgym:Digits Permutation(DFS)
- Repeater中OnItemDataBound使用方式
- java 反射的理解和使用
- Android之二维码生成与识别 读取相册
- 深入理解JavaScript系列(45):代码复用模式(避免篇)
- matlab调用java代码
- 对象分割--Instance-sensitive Fully Convolutional Networks
- 算法相关文章索引(3)
- win7,win8个人用户如何防御勒索病毒(Wanna Decryptor)?
- MySQL InnoDB :事务隔离隔离级别以及对应的问题