如何在asp转发器中查找标签

发布于 2024-09-05 04:13:52 字数 496 浏览 2 评论 0原文

我的 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");

但是当然,每次都是相同的标签(不一定是需要更新的标签)。我需要更新与按钮位于同一行的标签。

任何指导将不胜感激。

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)Repeater1.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舟遥客 2024-09-12 04:13:52

处理中继器的 ItemCommand 事件。在事件处理程序中,检查事件参数的 Item 属性并对其使用 findcontrol。例如,

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

Label1 将是与单击的按钮相同的项目中的标签。

或者响应 Dr. Wily 的学徒的评论,您可以执行以下操作

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”

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"

花开柳相依 2024-09-12 04:13:52

您将需要一个辅助方法来遍历层次结构或使用
Control 的 FindControl(string id) 方法即可实现此目的。

例子:

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

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");
零度° 2024-09-12 04:13:52

我假设按钮有一个事件处理程序?如果是这样,你应该能够做到

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

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文