Asp.Net Button Click Event inside Repeater inside UpdatePanel

I'm trying to use a Repeater inside an Updatepanel and has buttons that delete one database entry and get new data for the repeater and update the panel. I tried LinkButton but it always postbacks and reorders the page. Then I tried a regular button and create an event for that button on the DataBound event. But it doesn't work. Here is the code:

aspx file:

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


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

                <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>
                        <asp:Button runat="server" ID="test"/>



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

    protected void deleteMeldung(object sender, EventArgs e) {

    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 what works is passing text and id to each button. But buttons don't have a Click event to call deleteMeldung () method


source to share

2 answers

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

Buttons inside the repeater do not trigger feedback themselves, but they only set the given string ID to the hidden field HF_DeleteId and then call $ ('# B_Delete'). click () (where B_Delete should be replaced with the current ClientID of the button). Then, in the server's B_Delete_Click method, you can get the id of the row to be removed from the hidden field. No problem with update panels. No need to mess with triggers and handle events from dynamically generated buttons.

It might look like this:

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

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


And 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;",                                         
                 DataBinder.Eval(e.Item.DataItem, "ID"),

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




I tried Pafkas solution and it worked, thanks for that. However, I was intensified if there is any other way to solve this problem and I found another solution, I registered each LinkButton with a ScriptManager like this:

        btn1.Click += new EventHandler((sender1, e1) => deleteMeldung(sender1, e1, meldungId));

        ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
        if (scriptManager != null) {


and with this link button:

<asp:LinkButton runat="server" ID="test"/>


it worked, it fired the deleteMeldung method without reinstalling and passed the meldungId parameter to remove enterie and update the new data to UpdatePanel



All Articles