Click here to Skip to main content
15,886,795 members
Articles / UpdatePanel
Article

How to Postback Asynchornously from inside a GridView

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
11 Oct 2013CPOL4 min read 50K   5   1
ConceptWith the rise of ASP.NET 2.0 AJAX Extensions 1.0, developers were fascinated by the great AJAX Server controls, but they missed that some of

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Concept

With the rise of ASP.NET 2.0 AJAX Extensions 1.0, developers were fascinated by the great AJAX Server controls, but they missed that some of them might be harmful if they didn't really know how to use them in a proper way. One example is when there is a need to have a button (Select button) in every row, that when clicked shall cause an asynchronous postback to update an area on the page that is surrounded by an UpdatePanel.

A solution might be to place the entire GridView inside an UpdatePanel, and since the UpdatePanel has a property called "ChildrenAsTriggers" that has a value of "true" by default, then any postback that goes out of the GridView would cause an asynchronous postback.

But wait!!

Doesn't this mean that every time, a button in a specific row is clicked, the entire GridView is posted-back to the server and then again on the way back, GridView is refreshed? Yes, this is true and that's why I mentioned above, AJAX Server Controls should be used in a proper way, i.e. Use but Don't Over Use!!

Solution

The situation is as follows. There is an UpdatePanel with a Label inside that holds the current DateTime value. Below this UpdatePanel there is a GridView that lists data from the Vendors data table located in the AdventureWorks Database. For every row, there is a Select Button that will should cause an asynchronous postback and hence updating the Label located in the above UpdatePanel. Here is the ASPX markup:

<br />    <asp:ScriptManager ID="ScriptManager1" runat="server"><br />    </asp:ScriptManager><br />   <br />    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"><br />    <ContentTemplate><br />        <asp:Label ID="lblName" runat="server" /><br />    </ContentTemplate><br />    </asp:UpdatePanel><br />    <br />    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" <br />        AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound"><br />        <Columns><br />            <asp:BoundField DataField="ActiveFlag" HeaderText="ActiveFlag" <br />                SortExpression="ActiveFlag" /><br />            <asp:BoundField DataField="VendorID" HeaderText="VendorID" <br />                InsertVisible="False" ReadOnly="True" SortExpression="VendorID" /><br />            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /><br />            <br />            <asp:TemplateField HeaderText="Command"><br />                <ItemTemplate><br />                    <asp:UpdatePanel ID="gridUpdatePanel" runat="server"><br />                        <ContentTemplate><br />                            <asp:LinkButton ID="lbtnSelect" Text="Select" runat="server" <br />                                CommandName="Select" onclick="lbtnSelect_Click" /><br />                        </ContentTemplate><br />                    </asp:UpdatePanel><br />                </ItemTemplate><br />            </asp:TemplateField><br />        </Columns><br />    </asp:GridView><br />    <asp:SqlDataSource ID="SqlDataSource1" runat="server" <br />        ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" <br />        SelectCommand="SELECT TOP 5 [ActiveFlag], [VendorID], [Name] FROM [Purchasing].[Vendor]" />

The smart idea is to place only the column that holds the Select Button in an UpdatePanel, notice the bold area in the code above. Then when this LinkButton is clicked, an Async Postback will proceed and update the content of the Label inside the main UpdatePanel. In addition, you need to set the UpdateMode on the first UpdatePanel to "Conditional" so that not every Async postback on the page causes it to refresh its content.

What is left is to show the code behind of the LinkButton's event handler in C#:

    protected void lbtnSelect_Click(object sender, EventArgs e)<br />    {<br />        this.lblName.Text = DateTime.Now.ToString();<br />        this.UpdatePanel1.Update();<br />    } 

As you can see a call to the UpdatePanel's Update() method is executed. This will cause the UpdatePanel1 to have its content refreshed on the client-side. As you saw before, the UpdatePanel didn't define any triggers and had its UpdateMode set to Conditional, so the only way to get its content refreshed is to call its Update() method manually.

When testing this code in FireFox and turning on Firebug, we can see the response coming back from the server as:

62|updatePanel|UpdatePanel1| <span id="lblName">2/18/2008 6:51:14 PM</span> |165|updatePanel|GridView1_ctl02_gridUpdatePanel| <a id="GridView1_ctl02_lbtnSelect" href="javascript:__doPostBack('GridView1$ctl02$lbtnSelect'
,'')">Select</a> |165|updatePanel|GridView1_ctl03_gridUpdatePanel| <a id="GridView1_ctl03_lbtnSelect" href="javascript:__doPostBack('GridView1$ctl03$lbtnSelect' ,'')">Select</a> |165|updatePanel|GridView1_ctl04_gridUpdatePanel|
<a id="GridView1_ctl04_lbtnSelect" href="javascript:__doPostBack('GridView1$ctl04$lbtnSelect','')">Select</a> |165|updatePanel|GridView1_ctl05_gridUpdatePanel|
<a id="GridView1_ctl05_lbtnSelect" href="javascript:__doPostBack('GridView1$ctl05$lbtnSelect','')">Select</a> |165|updatePanel|GridView1_ctl06_gridUpdatePanel|
<a id="GridView1_ctl06_lbtnSelect" href="javascript:__doPostBack('GridView1$ctl06$lbtnSelect' ,'')">Select</a> |0|hiddenField|__EVENTTARGET||0|hiddenField|__EVENTARGUMENT||704|hiddenField
|__VIEWSTATE/ViewState cut down 

 

Oh, what went wrong? You can see that the entire GridView was refreshed! This defeats the purpose of what has been mentioned above!! Well, you never learn until you do the mistake! Let us think about it a bit, every row now holds an UpdatePanel, every UpdatePanel has the UpdateMode="Always" and ChildrenAsTriggers="True" -- Default values. This clearly explains why the entire GridView was refreshed upon clicking on the Select LinkButton.

How to solve this?

All what you need to do is add the following to the UpdatePanel located inside the GridView:

 UpdateMode="Conditional" ChildrenAsTriggers="false" 

This tells the UpdatePanels located inside the GridView, not to refresh on every Async Postback and not to get refreshed by any event that occurs from within the contents of the UpdatePanel. In other words, never refresh this UpdatePanel. This is true as long as you don't care about updating anything inside the GridView row. This is a good solution as long as what you want to peform is refresh other content outside the GridView.

Now, running the same code again and testing using FireFox and Firebug, you can see how the response has been cut down to only the ViewState and the content of the upper UpdatePanel that contains the Label to be updated:

62|updatePanel|UpdatePanel1| <span id="lblName">2/18/2008 7:01:01 PM</span>    |0|hiddenField|__EVENTTARGET||0|hiddenField|__EVENTARGUMENT||704|hiddenField|__VIEWSTATE| ViewState cut down

Can you see how much you have gained by adding those two properties? Now you have your problem solved with a GridView containing a button to update an external UpdatePanel and with a good performance hit! Imagine how helpful this would be for a larger amount of data being displayed in the GridView?

 

Hope you enjoyed this article!
Regards

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
GeneralComment Pin
sumit19858512-Jun-14 0:53
sumit19858512-Jun-14 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.