简体   繁体   English

在中继器中找不到链接按钮

[英]Can't find a link button in a repeater

I have a link button in a repeater with a couple of databound fields.我在带有几个数据绑定字段的转发器中有一个链接按钮。 I'm trying to get to where I can set the buttons onClientClick after the databinding however everytime I try to access the link button I keep getting Null returned.我试图在数据绑定之后到达可以设置按钮 onClientClick 的位置,但是每次我尝试访问链接按钮时,我都会不断返回 Null。

I've looked through every single question involving repeaters and controls here and haven't been able to figure it out.我在这里查看了涉及中继器和控件的每一个问题,但无法弄清楚。

The.aspx .aspx

    <asp:Repeater ID="DailyRepeater" OnItemCommand="DailyRepeater_ItemCommand" runat="server">
                    <HeaderTemplate>
                        <tr>
                            <td class="coltitle">
                                Time
                            </td>
                            <td class="coltitle">
                                Activity
                            </td>
                            <td class="coltitle">
                                Hours
                            </td>
                        </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr class="evenrow">
                            <td>
                                <%# Eval("StartTime","{0:HH:mm}") %>-<%# Eval("EndTime","{0:HH:mm}") %>
                            </td>
                            <td>
                                <%# Eval("Description") %>
                            </td>
                            <td>
                                <%# Eval("Hours","{0:0.0}") %>
                            </td>
                            <td>
                                   <asp:LinkButton runat="server" CausesValidation="false" ID="editbutton" Text="Edit">Edit</asp:LinkButton>
                            </td>
                        </tr>
                    </ItemTemplate>

The.aspx.cs .aspx.cs

    protected void DailyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton myButton = (LinkButton)e.Item.FindControl("editbutton");
        myButton.OnClientClick = (popupWindow.GetTargetPopupCode("URL");
    }

From what I've read, I should have been able to get to the button using the RepeaterItemEventargs.根据我的阅读,我应该能够使用 RepeaterItemEventargs 访问按钮。 However I can't seem to find it here.但是我似乎无法在这里找到它。 The other thing I thought of was that the binding wasn't happening by the time this happened (which made no sense to be as this is a databound event) but for some reason e is coming back e.Item has a dataItem of null and an itemIndex of -1...我想到的另一件事是,在发生这种情况时绑定还没有发生(这没有任何意义,因为这是一个数据绑定事件)但由于某种原因 e 回来了 e.Item 的 dataItem 为 null 和-1 的 itemIndex ...

I'm just really confused and lost any help would be greatly appreciated.我真的很困惑,失去任何帮助将不胜感激。

Thanks!谢谢!

It sounds like you haven't guarded against the item type.听起来您还没有防范项目类型。 Typically, it fires the header, all the items, and then the footer.通常,它会触发 header、所有项目,然后是页脚。 You need to do this:你需要这样做:

 if(e.Item.ItemType == ItemType.Item || e.Item.ItemType == ItemType.AlternatingItem)
 {
      LinkButton myButton = (LinkButton)e.Item.FindControl("editbutton");
      myButton.OnClientClick = (popupWindow.GetTargetPopupCode("URL");
 }

Why not setting the property on the sender object, like this:为什么不在sender object 上设置属性,像这样:

sender.OnClientClick = //whatever//

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

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