yuanweisen

 

使用Repeater实例

1 首先,也是最重要的,就是要在aspx页面引入引用代码:<%@   Import   Namespace="System.Data"   %>

2 在页面上加控件Repeater

 <asp:Repeater ID="Rep_Data" runat="server" OnItemDataBound="Rep_Data_ItemDataBound" OnItemCommand="Rep_Data_ItemCommand">
                       <ItemTemplate>
                       <asp:Label ID="LB_Id" runat="server" Text='<%# ((DataRowView)Container.DataItem)["MC_Id"] %>' Visible="false"></asp:Label>
                       <asp:LinkButton ID="LB_Title" runat="server" Text='<%# ((DataRowView)Container.DataItem)["MC_Title"] %>' CommandName="Display">
                      
                       </asp:LinkButton>
                      <br>
                       </ItemTemplate>
                       </asp:Repeater>

3 其实Repeater和GridView用法一样,都要有RowBind和Command事件

 

 /// <summary>
        /// Repeater数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Rep_Data_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //把链接列按钮设置传入命令参数
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton LB_Title = e.Item.FindControl("LB_Title") as LinkButton;
                Label LB_Id=e.Item.FindControl("LB_Id") as Label;
                LB_Title.CommandArgument = LB_Id.Text.ToString();
            }
           
        }

        protected void Rep_Data_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "Display")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                Business.MedicalKnowledgeClass MyKLC = new Business.MedicalKnowledgeClass();
                MyKLC = Business.MedicalKnowledgeClass.GetModel(index);
                this.txt.InnerHtml = MyKLC.MC_Text;
                RegisterStartupScript("", "<script type='text/javascript'>displays();</script>");


                this.Pn_Display.Visible = true;
                this.Pn_GridView.Visible = false;

            }
        }

总结起来,就是在Repeater里放两个控件,好像Repeater暂时还没有DataKeys的属性,所以要传命令参数只能先用一个控件来承载,其它的用法和GridView都差不多

二 一般在Repeater里绑定的不会是服务器控件,更多的是超链接,

 <asp:Repeater ID="Rep_Data" runat="server">
                       <ItemTemplate><li><a href='HealthEducationDisplay.aspx?action=Display&Id=<%# Eval("HE_Id") %>'><%# Eval("HE_Question")%></a><span style="float:right;height : 22px; line-height: 22px"><%# Eval("HE_CreateDate") %></span></li>
                       </ItemTemplate>
                       </asp:Repeater>

这里面定义的样式完全可以定义到项目中的CSS文件里

然后在pageload事件里先判断传入的参数是什么,来进行什么操作。

<ItemTemplate><li><a href='HealthEducationDisplay.aspx?action=Display&Id=<%# Eval("HE_Id") %>'><%# Xkzi.Common.Utils.StringPlus.ClipString(Eval("HE_Question", "{0}"), 11)%></a><span style="float:right;height : 22px; line-height: 22px"><%# DateTime.Parse(Eval("HE_CreateDate","{0}")).ToString("yyyy-MM-dd") %></span></li>

另外一种常用到的绑定方式

 

posted on 2008-12-01 19:48    阅读(841)  评论(0编辑  收藏  举报

导航