Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full postback triggered by LinkButton inside GridView inside UpdatePanel

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> 
like image 239
Kevin Albrecht Avatar asked Feb 02 '11 08:02

Kevin Albrecht


People also ask

What is postback trigger in 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).

What is async postback trigger?

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.

What is use of update panel in asp net?

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.


2 Answers

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

like image 193
NakedBrunch Avatar answered Oct 02 '22 17:10

NakedBrunch


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.

like image 25
Ewert Avatar answered Oct 02 '22 19:10

Ewert