How to get literal content value in Repeater

天涯浪子 提交于 2019-12-06 11:10:38
Kapil Khandelwal

Try this,

foreach (RepeaterItem item in rptReports.Items)
{
    Label lblReportID= (Label)item.FindControl("lblReportID");
    string ReportID = lblReportID.Text;
}

If you are using "div", then try this:

 <div id="hiddenContent2" runat="server"> 
        <%# Eval("ReportID") %>
 </div>

 foreach (RepeaterItem item in rptReports.Items)
 {
     System.Web.UI.HtmlControls.HtmlGenericControl hiddenContent2 = (System.Web.UI.HtmlControls.HtmlGenericControl)item.FindControl("hiddenContent2");
     string ReportID = hiddenContent2.InnerHtml;
 }

You could use ItemIndex property of Repeater. This is the sample:

<asp:Repeater runat="server" ID="repTest">
    <ItemTemplate>
        <div id="hDiv">
            <%# Eval("SomeID") %>
        </div>
    </ItemTemplate>
</asp:Repeater>

Code behinde:

protected void Page_Load(object sender, EventArgs e)
{
    var objects =
        new object[] {
            new {SomeID = 1},
            new {SomeID = 2},
            new {SomeID = 3},
            new {SomeID = 4}
        };

    repTest.DataSource = objects;
    repTest.DataBind();
}

protected void btnClick(object sender, EventArgs e)
{
    var data = (object[])repTest.DataSource;
    foreach (RepeaterItem item in repTest.Items)
    {
        var obj = data[item.ItemIndex];
        var id = obj.GetType().GetProperty("SomeID").GetValue(obj, null);
    }
}

So, two things you need to do.

  • cast DataSource to your type. (I used object[] just for sample).
  • cast data[item.ItemIndex] to your type. I just use reflection, 'cause I have anonimus type, so if you have a type, you could cast it.
  • 易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!