I have a GridView inside of a UpdatePanel. In a template field is a button I use for marking items. Functionally, this works fine, but the button always triggers a full page postback instead of a partial postback. How do I get the button to trigger a partial postback?
<asp:ScriptManager ID="ContentScriptManager" runat="server" /> <asp:UpdatePanel ID="ContentUpdatePanel" runat="server" ChildrenAsTriggers="true"> <ContentTemplate> <asp:GridView ID="OrderGrid" runat="server" AllowPaging="false" AllowSorting="false" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText=""> <ItemTemplate> <asp:LinkButton ID="MarkAsCompleteButton" runat="server" Text="MarkAsComplete" CommandName="MarkAsComplete" CommandArgument='<%# Eval("Id") %>' /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="LoadDate" HeaderText="Load Date" /> <asp:BoundField DataField="EmployeeCutOffDate" HeaderText="Cut Off Date" /> <asp:BoundField DataField="IsComplete" HeaderText="Is Completed" /> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel>
<asp:AsyncPostBackTrigger> Specifies a control and event that will cause a partial page update for the UpdatePanel that contains this trigger reference. <asp:PostBackTrigger> Specifies a control and event that will cause a full page update (a full page refresh).
Use the AsyncPostBackTrigger control to enable controls to be triggers for an UpdatePanel control. Controls that are triggers for an update panel cause a refresh of the panel's content after an asynchronous postback.
Introduction. UpdatePanel controls are a central part of AJAX functionality in ASP.NET. They are used with the ScriptManager control to enable partial-page rendering. Partial-page rendering reduces the need for synchronous postbacks and complete page updates when only part of the page has to be updated.
You need to register each and every LinkButton as an AsyncPostBackTrigger
. After each row is bound in your GridView, you'll need to search for the LinkButton and register it through code-behind as follows:
protected void OrderGrid_RowDataBound(object sender, GridViewRowEventArgs e) { LinkButton lb = e.Row.FindControl("MarkAsCompleteButton") as LinkButton; ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb); }
This also requires that ClientIDMode="AutoID"
be set for the LinkButton, as mentioned here (thanks to Răzvan Panda for pointing this out).
It's probably not advised but you can make everything on the GridView work asynchronously by excluding the EventName on the AsyncPostBackTrigger so e.g.
<Triggers> <asp:AsyncPostBackTrigger ControlID="OrderGrid" /> </Triggers>
This will make the RowCommand event and any other event on the GridView fire asynchronously. Note as well that when you make ClientIDMode="Static" on the GridView it will cause a full postback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With