C# – Asp.Net Button click Event inside Repeater inside UpdatePanel

asp.netceventsrepeaterupdatepanel

I am trying to use a Repeater inside a Updatepanel and have Buttons that delete one entery in the Database and get the new Data for the Repeater and Update the Panel. I have tried a LinkButton, but it does always postback and the page relodes. Then i tried a regular Button and create a Event for that Button on DataBound Event. But it doesnt work. Here is the code:

aspx file:

 <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server" id="Panel">
        <ContentTemplate> 
            <table cellpadding="0" cellspacing="0" id="saveTable">
                <tr style="font-weight: bold;">
                    <td>Erstellt am</td>
                    <td>Anforderer</td>
                    <td>Werk</td>
                    <td>Gebäude</td>
                    <td>Start Datum</td>

                    <td>Löschen</td>
                    <td>Benutzen</td>
                </tr> 


            <asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
                <ItemTemplate>

                <tr id="Meldung<%# DataBinder.Eval(Container.DataItem,"meldungId")%>">
                    <td><%# DataBinder.Eval(Container.DataItem, "timestamp").ToString().Substring(0, 10)%></td>
                    <td><%# DataBinder.Eval(Container.DataItem,"nameAnforderer") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem,"Werk") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem,"Building") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem,"startDatum").ToString().Substring(0, 10) %></td>
                    <td>
                        <asp:Button runat="server" ID="test"/>
                    </td>
                    <td></td>
                </tr>

               </ItemTemplate>
            </asp:Repeater>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>

and in the code behind i give the Button with id = test a event

    protected void deleteMeldung(object sender, EventArgs e) {
        System.Threading.Thread.Sleep(5000);
    }

    protected void Repeater1_ItemDataBound(object source, RepeaterItemEventArgs e) {
        RepeaterItem repeaterSource = e.Item;
        Button btn1 = repeaterSource.FindControl("test") as Button;
        DataRowView drv = e.Item.DataItem as DataRowView;
        string meldungId = drv.Row["meldungId"].ToString();

        btn1.ID = "myNewButton"+meldungId;
        btn1.Click += new EventHandler(deleteMeldung);
        btn1.Text = "delete";

    }

so whats working is passing the Text and the ID to each button like that. But the Buttons do not have the Click Event to call deleteMeldung()

Best Answer

What I tend to do is to have a separate delete button outside of the repeater (this is usually asp:Button with style set to 'display:none' along with a hidden field. Lets call these B_Delete and HF_DeleteId.

The buttons inside of the repeater do not cause postback themselves, but they only set the ID of the given row to the hidden field HF_DeleteId, and then call $('#B_Delete').click() (where the B_Delete should be replaced with current ClientID of the button). Then in the server method B_Delete_Click you can retrieve the ID of the row to be deleted from the hidden field. Works with update panels no problem. No need to mess with triggers and handling events from dynamically generated buttons.

It may look like this:

<asp:UpdatePanel runat="server" ID="UP_Rptr" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Repeater runat="server" ID="RPTR_DeleteTest" EnableViewState="False" OnItemDataBound="RPTR_DeleteTest_ItemDataBound">
            <ItemTemplate>
                <div>
                    <span><%# Eval("ID") %></span>
                    <span><%# Eval("Name") %></span>
                    <span><asp:LinkButton runat="server" ID="LB_Delete" Text="delete"></asp:LinkButton></span>
                </div>
            </ItemTemplate>
        </asp:Repeater>

        <asp:HiddenField runat="server" ID="HF_DeleteId" />        
        <asp:Button runat="server" ID="B_Delete" OnClick="B_Delete_Click" Style="display:none;" />
    </ContentTemplate>
</asp:UpdatePanel>

And the server methods:

protected void RPTR_DeleteTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   var lb = e.Item.Controls[1] as LinkButton; // for simplicity only - do not use code like this
   lb.OnClientClick = String.Format("$('#{0}').val('{1}');$('#{2}').click();return false;",                                         
                 HF_DeleteId.ClientID, 
                 DataBinder.Eval(e.Item.DataItem, "ID"),
                 B_Delete.ClientID);
}

protected void B_Delete_Click(object sender, EventArgs e)
{
    int id = Int32.Parse(HF_DeleteId.Value);
    // do your sanity checks and deletion logic here
}
Related Topic