简体   繁体   English

事件绑定到Repeater的每个LinkBut​​ton不起作用

[英]Event binding to each LinkButton of Repeater not working

I need set a button for each item of repeater in C#(asp.net). 我需要为C#(asp.net)中的每个转发器设置一个按钮。

<com:Repeater runat="server" ID="list_repeater">
  <ItemTemplate>
    <tr>
      <td>
        <asp:LinkButton runat="server" ID="btnCancel" CommandArgument='<%# Eval("id") %>'>Cancel</asp:LinkButton>
      </td>
      ...
    </tr>
  </ItemTemplate>
</com:Repeater>

I bind data in PageLoad. 我在PageLoad中绑定数据。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        list_repeater.DataSource = ... getData ..;
        list_repeater.DataBind();
        ...
    }
}

Of course, i bind RepeaterItemEventHandler on the list. 当然,我在列表上绑定RepeaterItemEventHandler。

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    base.LoadingControlAdd();
    ...
    list_repeater.ItemDataBound += new RepeaterItemEventHandler(list_repeater_ItemDataBound);
    ...
}

and then i bind EventHandler to each btnCancel. 然后我将EventHandler绑定到每个btnCancel。

protected void list_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        LinkButton btnCancel = e.Item.FindControl("btnCancel") as LinkButton;
        btnCancel.Click += new EventHandler(btnCancel_Click);
    }
}

But, it does not work. 但是,它不起作用。 When i click the btnCancel of each item, btnCancel_Click is not triggered. 当我单击每个项目的btnCancel时 ,不会触发btnCancel_Click I think i does well, and can't find any flaw. 我认为我做得很好,并且找不到任何缺陷。 Is there any fault on the code above please tell me what's wrong. 上面的代码是否有任何错误,请告诉我有什么问题。 Thanks. 谢谢。 (id on CommnadArgument is set properly, don't mind it) (CommnadArgument上的ID设置正确,请不要介意)

I Solved. 我解决了。

Refer: https://forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater 参考: https : //forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater

As far as I know, "itemDataBound" event is used when you call DataBind(). 据我所知,当您调用DataBind()时会使用“ itemDataBound”事件。 However, a databind process can usually be done in the body of if (!IsPostBack){……},So you can never call DataBind() any more, I guess the reason is this. 但是,数据绑定过程通常可以在if(!IsPostBack){……}的主体中完成,因此您再也不能调用DataBind()了,我想原因是这样的。

To solve the problem, You can try to put the codes like this: 要解决该问题,您可以尝试将如下代码放入:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        list_repeater.DataSource = ... getData ..;
        list_repeater.DataBind();
        ...
    }
    ...
    foreach(RepeaterItem item in list_repeater.Items) {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton btnCancel = item.FindControl("btnCancel") as LinkButton;
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }
    }

It's caused by my poor knowledge for asp.net. 这是由于我对asp.net的了解不足所致。 Sorry for self-answering in minutes after post the question. 抱歉,问题发表后几分钟之内即可自动答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM