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

+3


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">
    <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 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
}

      

+2


source


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) {
            scriptManager.RegisterAsyncPostBackControl(btn1);
        }

      

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

0


source







All Articles