我的asp:repeater的结构
repeater
updatePanel
label1 (rating)
button (updates rating)
some_picture (thing being rated)
/update panel
/repeater
想象一下上述转发器的输出包含100行。 (1个标签,每行1个按钮)。
目标:当我点击按钮时,我希望更新相应的标签。我不知道该怎么做。 我可以通过以下方式引用标签:
Label myLabel2Update = (Label)Repeater1.Controls[0].Controls[0].FindControl("Label1");
但是,当然,每次都是相同的标签(不一定是需要更新的标签)。我需要更新与按钮位于同一行的标签。
任何指导都将不胜感激。
答案 0 :(得分:3)
处理转发器的ItemCommand事件。在事件处理程序中,检查事件参数的Item属性,并在其上使用findcontrol。 e.g。
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label Label1 = (Label)e.Item.FindControl("Label1");
}
Label1将是与单击按钮相同的项目中的标签。
或者回应Wily博士的Apprentice的评论,你可以做以下
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;
}
}
然后为每个按钮指定命令名称“bClick”
答案 1 :(得分:0)
您需要一个辅助方法来遍历层次结构或使用
控制FindControl(string id)
方法。
示例:
var stateLabel = (Label)e.Row.FindControl("_courseStateLabel");
答案 2 :(得分:0)
我假设按钮有一个事件处理程序?如果是这样,你应该能够做到
protected virtual void OnClick(object sender, EventArgs e)
{
var label = ((WebControl)clickedButton).Parent.FindControl("Label1");
}