asp.net repeater - get value of row item _DataBound

时光怂恿深爱的人放手 提交于 2019-12-04 06:16:20

问题


how can I get the value of each cell that I want in a repeater _DataBound sub? - this is so I can change the value a little and apply the change to a literal


回答1:


Let's say this is your repeater (since you didn't include any code):

<asp:Repeater ID="_r" runat="server">
    <ItemTemplate>
        <asp:Literal ID="_lit" Text='<%# Eval("yourItemName")%>' runat="server"></asp:Literal>
        <br /><br />
    </ItemTemplate>
</asp:Repeater>

and you make an ItemDataBound event because you want to add "meow" to the end of every literal (which otherwise will just show the value of yourItemName from the datasource):

protected void Page_Load(object sender, EventArgs e)
{
    _r.DataSource = table;
    _r.ItemDataBound += new RepeaterItemEventHandler(RItemDataBound);
    _r.DataBind();
} 

protected void RItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal lit = (Literal)e.Item.FindControl("_lit");
        lit.Text += "meow";
    }
}

Reading: Repeater.ItemDataBound Event




回答2:


You can get the literal in that row but casting the event arg:

Protected Sub repeater_dataBind(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
    If (e.Item.ItemType <> ListItemType.Item And e.Item.ItemType <> ListItemType.AlternatingItem) Then
            Return
    Else
       Dim myLiteral As New Literal
       myLiteral = CType(e.Item.FindControl("IDofLiteral"), Literal)
    End If

end Sub



回答3:


You can use FindControl to get the values of a cell inside a repeater.

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal lbl = (Literal)e.Item.FindControl("ltrl");  
        lbl.Text = // will give you the value for each `ItemIndex`

    }
}


来源:https://stackoverflow.com/questions/15688704/asp-net-repeater-get-value-of-row-item-databound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!