开发者

How to find a label in a asp repeater

Structure of my asp: repeater

repeater
    updatePanel
         label1 (rating)
         button (updates rating)
         some_picture (thing being rated)
     /update panel
/repeater

Imagine the output of the above repeater containing 100 rows. (1 label, and 1 button on each row).

Goal: when I click the button, I want the appropriate label to be updated. I dont know how to do this. I can reference a label via:

Label myLabel2Update = (Label)R开发者_开发百科epeater1.Controls[0].Controls[0].FindControl("Label1");

But ofcourse, it will be the same label each time (not necessarily the label that needs to be updated). I need to update the label that is on the same row as the button.

Any guidance would be appreciated.


Handle the ItemCommand event of the repeater. In your event handler check the Item property of the event arguments and use findcontrol on that. e.g.

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    Label Label1 = (Label)e.Item.FindControl("Label1");
}

Label1 will be the label in the same item as the button that was clicked.

Or in response to Dr. Wily's Apprentice's comment you could do the following

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "bClick":
            Label Label1 = (Label)e.Item.FindControl("Label1");
            /*do whatever processing here*/
            break;            
    }
}

And then for each button specify the command name "bClick"


You will need a helper method to iterate through the hierarchy or use the Control's FindControl(string id) method for that.

Example:

var stateLabel = (Label)e.Row.FindControl("_courseStateLabel");


I assume there's an event handler for the button? If so, you should be able to do

protected virtual void OnClick(object sender, EventArgs e)
{
    var label = ((WebControl)clickedButton).Parent.FindControl("Label1");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜