简体   繁体   中英

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.

I've looked through every single question involving repeaters and controls here and haven't been able to figure it out.

The.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

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

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. 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.OnClientClick = //whatever//

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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