locked
Gridview inside UpdatePanel, paging/sorting doesn't work ONLY when using MasterPage RRS feed

  • Question

  • User-59642099 posted

    Hi all,

    Gridview looks a bit like this:

    <asp:UpdatePanel runat="server"
    ID="upGdvPendingReview"
    ChildrenAsTriggers="true"
    UpdateMode="Conditional">
      <ContentTemplate>
         <asp:GridView
              ID="gdvPendingReview"
              runat="server"                                                            
              PageSize="10"
              AllowPaging="true"
              AllowSorting="true"
              >
               <columnCrudHere>                                             
         </asp:GridView>
       </ContentTemplate>
    </asp:UpdatePanel>

    Gridview is bound to linqdatasource using the Selecting event. This code works perfectly without the updatepanel. It also works perfectly if I copy to a page that isn't a Content Page to a Master Page. I've read a lot of posts about gridview issues in update panels with paging and sorting. In fact, there is one where they guy comments at the end that he got it working but it still fails when using a MasterPage. I've tried using a scriptmanager in the same page as the gridview and changing various options of the scriptmanager. To get it working, I just create a new page, copy my gridview, linqdatasource, scriptmanager, and code behind...and boom it works. It makes me wonder if I have something else in the page getting in the way. I do have other updatepanels where I can update content fine. So, this...combined with the post where the same issue is mentioned briefly has me perplexed.

    I've also tried explicitly listing the event:

    <Triggers>
      <asp:AsyncPostBackTrigger ControlID="gdvPendingReview" EventName="PageIndexChanging" />
    </Triggers>

    As I continue to test this, it becomes more frustrating and fascinating. I have created two new pages with only the gridview, linqdatasource, scriptmanager, and databinding methods. One page has no master....the other page has a master (but a completely new and clean one to avoid any possible interference). Same results! Paging/Sorting works great asynchronously as long as I'm not using a master page. :(

    Thanks all!

    Sunday, December 22, 2013 2:29 PM

All replies

  • User1734617369 posted

    Hi,

    I have created a test site to try to see the problem that you have. Tha master page looks like this:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="WebApplication6.MasterPage" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="SM1"></asp:ScriptManager>
            <div>
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> 
                </asp:ContentPlaceHolder>
            </div>
        </form>
    </body>
    </html>

    and the content page with code:

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:UpdatePanel runat="server" ID="UP" UpdateMode="Conditional" ChildrenAsTriggers="True">
            <ContentTemplate>
                <asp:GridView runat="server" ID="GW" PageSize="20" OnPageIndexChanging="GW_OnPageIndexChanging" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1">                
                    <Columns>
                        <asp:BoundField DataField="CountryId" HeaderText="CountryId" ReadOnly="True" SortExpression="CountryId" />
                        <asp:BoundField DataField="CountryName" HeaderText="CountryName" ReadOnly="True" SortExpression="CountryName" />
                    </Columns>
                </asp:GridView>           
                <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="WebApplication6.MSSEntities" EntityTypeName="" OnSelecting="LinqDataSource1_OnSelecting">
                </asp:LinqDataSource>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Content>
    using System;
    using System.Linq;
    using System.Web.UI.WebControls;
    
    namespace WebApplication6
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {            
            }
    
            protected void GW_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                
            }
    
            protected void LinqDataSource1_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
            {
                var ctx = new MSSEntities();
                
                    var countries = from country in ctx.dim_Country orderby country.CountryName select country;
                    e.Result = countries;
                            
            }
        }
    }

    This works without any problems so I wonder what the main differences are from your code so that I may see if I can find the error?

    Best regards
    Johan

    Monday, December 23, 2013 4:20 AM
  • User-59642099 posted

    Thank you for taking the time to do that. So, I just re-learned an important lesson.

    Remember the test page I set up to test the issue where I took only the related code so there would be "no interference" from anything else on the page. Well, I had some code in that page from a previous test. I went back and removed EVERYTHING except the bare minimum needed for this test and everything worked just as it did for you. LESSON LEARNED (AGAIN): When you test, don't assume anything and test in as much of a pristine environment as you can.

    But the question remains, what is causing my code NOT to work? In my test page, I found that I had code in the Page_Load that was executing a "Response.Write" from a previous test. The "Response.Write" breaks the gridview paging in the UpdatePanel. I have toggled that line off and on to verify it is the culprit.

    I went back to my original page, but I don't have anything like that. So far, I cannot find what is causing the issue there, but I know that something is causing it, so I can methodically work at this.

    Can anyone explain what the Response.Write is doing to the page to break this?

    Thanks!

    Monday, December 23, 2013 12:50 PM
  • User-417640953 posted

    Can anyone explain what the Response.Write is doing to the page to break this?

    Hi mattdaddym2,

    Thank you post the issue to asp.net forum.

    According to your description, I see that you were using the Response.Write when the updatepanel async post back.

    As we all know that we can't use Response.Write during an asynchronous postback. If you want to use it when gridview paging or sorting.

    I think you should set it as PostBackTrigger of updatepanel. Like below:

     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
                        <Columns>
                            <asp:BoundField DataField="ID" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="Button2" runat="server" Text="Button" CommandName="xyz"  />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate> 
                <Triggers>
                    <asp:PostBackTrigger ControlID="Button1" />
                     <asp:PostBackTrigger ControlID="GridView1" />
                </Triggers>
     </asp:UpdatePanel>

    Code behind:

     protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write("Button1_Click!");
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                Response.Write("GridView1_RowCommand!");
            }

    Besides, you can also set it in the code behind like below.

     ScriptManager.GetCurrent(this).RegisterPostBackControl(GridView1);

    Thanks.

    Best Regards!

    Tuesday, December 24, 2013 2:20 AM
  • User-59642099 posted

    Hi,

    I wasn't using the response.write during an async postback. I executed during page_load. At that point the async postbacks in mygridview wouldn't work. Removing the Response.Write got things working in my test page, but I don't have any code like this in production. I'm also still curious why this happens.

    Also, I wasn't looking for a postback trigger. Making the gridview a postback trigger for itself seems like it would defeat the purpose of putting it in an updatepanel in the first place. The gridview should page and sort asynchronously, but it isn't. Something in my page is getting in the way. Luckily, I finally tracked it down.

    The answer to this issue is a bit embarrassing.

    Lesson 1 - When you test, you isolate your issue as completely as possible and do not assume anything.

    Lesson 2 - When troubleshooting AJAX issues, look for javascript errors.

    Turns out my code works fine. The issue was other updatepanels on the page set to update "always". I noticed a javascript error in Firebug alerting me that an updatepanel couldn't be found when trying to sort/page the gridview I was having issues with. When I try to page my gridview, any updatepanel set to always also tries to update. Well, there is a "hidden" section of my page (visibility is off) with updatepanels. When attempting to sort/page my gridview, javascript is looking for these, I assume to update them, but cannot find them. There is a javascript error and all progress stops. The solution was to change the updatepanels that are hidden all to conditional (which has other repercussions but its bearable).

    Does this seem like the correct behavior by default. If an updatepanel is inside another control whose visibility is turned off, should it be attempting to update?

    Thanks all.

    Tuesday, December 24, 2013 4:27 PM
  • User-417640953 posted

    Hi mattdaddym,

    I'm glad to see you post back.

    According to your description, I'm sorry to say I cannot reproduce the scenes you described.

    Would you mind share a simple demo with us and let us reproduce the scenes.

    Then we will provide further solutions for you. Thanks.

    Best Regards!

    Tuesday, December 24, 2013 8:10 PM