Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindControl inside Repeater

Tags:

c#

asp.net

I am trying to find a Label inside a Repeater, I am using:

    protected void lnkbtnEditRecord_Click(object sender, EventArgs e)
    {
        salesEditPanel.Visible = true;
        resultPanel.Visible = false;
        zipPanel.Visible = false;
        ddlPanel.Visible = false;
        topPanel.Visible = false;

        Label lblSalesId = (Label)(Repeater2.Items[0].FindControl("lblSalesID"));

        DataView dv = FillSalesPersonForm(Convert.ToInt32(lblSalesId.Text));

        frmViewSalesPeople.DataSource = dv;
        frmViewSalesPeople.DataBind();

    }

This works great if the repeater only has one result, but if it has more then one it only gets the id for the first item listed. Any ideas on how to fix this?

EDIT: Basically the return is filled with user info and with each user info is a link to another page. So when I click the link I want to get the ID from the label for the record the link was clicked for. So If I click record 4 out of 5 I want and the ID for record 4 is 900 I want to get the 900 to pass.

Thanks!


1 Answers

Try something like this:

foreach (RepeaterItem item in rptItems.Items)
{
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        var lbl= (Label)item.FindControl("lblMyLabel");

        lbl.Text = "do something to your label";
    }
}
like image 64
Induster Avatar answered Feb 01 '26 03:02

Induster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!