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>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 gta5ol寻宝任务退出了怎么办 gta5线上模式买房子之后怎么办 开摩托车忘记带安全头盔怎么办 gta不想要车了怎么办 空气滤芯进水会怎么办 车胎扎了个钉子怎么办 德罗索没导弹了怎么办 CF手雷包不能用怎么办 cfAK爆头碰到狙怎么办 玩cf网络延迟高怎么办 逆水寒装备分解错了怎么办 轴与孔间隙过大怎么办 小孩眼睛被打了怎么办 玩王者荣耀手机屏幕竖着怎么办 棉被被老鼠尿湿怎么办 打完子弹能下来怎么办 怪物猎人 弩子弹打完了怎么办 烤瓷牙龈发黑了怎么办 做彩超前喝水了怎么办 胸贴过敏红痒怎么办 芡粉里面有虫了怎么办 勾芡淀粉放多了怎么办 剁排骨没有好刀怎么办 硬币掉进档位里怎么办 魅族mx5手机太卡怎么办 华为m9手机声音小怎么办 放卡的地方堵住怎么办 美图m8蓝屏了怎么办 美图m8手机蓝屏怎么办 美图m6s手机白屏怎么办 魅蓝2开不开机怎么办 魅族手机主键没反应怎么办 魅族手机主键失灵怎么办 手机4g网络不稳定怎么办 华为m9收不到手机信息怎么办 华为手机wifi信号弱怎么办 手机连接wifi信号差怎么办 华华为p10信号不好怎么办 烟没拆封受潮了怎么办 和亲儿子发生了性关系怎么办 无心磨磨出来圆度不好怎么办