views:

911

answers:

2

I have a GridView that uses custom paging inside an UpdatePanel (so that the paging and sorting of the gridview don't cause postback). The sorting works fine, but the paging doesn't. The PageIndexChanging event is never called.

This is the aspx code:

<asp:UpdatePanel runat="server" ID="upSearchResults" ChildrenAsTriggers="true" UpdateMode="Always">
        <ContentTemplate>
          <asp:GridView ID="gvSearchResults" runat="server" AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" OnDataBound="gvSearchResults_DataBound"
                OnRowDataBound ="gvSearchResults_RowDataBound" OnSorting="gvSearchResults_Sorting" OnPageIndexChanging="gvSearchResults_PageIndexChanging" Width="100%" EnableSortingAndPagingCallbacks="false">
            <Columns>
              <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                  <asp:HyperLink ID="lnkAdd" runat="server">Add</asp:HyperLink>
                  <asp:HiddenField ID="hfPersonId" runat="server" Value='<%# Eval("Id") %>'/>
                </ItemTemplate>
              </asp:TemplateField>
              <asp:BoundField HeaderText="First Name" DataField="FirstName" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="FirstName" />
              <asp:BoundField HeaderText="Last Name" DataField="LastName"  HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="LastName" />
              <asp:TemplateField HeaderText="Phone Number" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                  <asp:Label ID="lblPhone" runat="server" Text="" />
                </ItemTemplate>
              </asp:TemplateField>
            </Columns>
            <PagerTemplate>
              <table width="100%" class="pager">
                <tr>
                  <td>
                  </td>
                </tr>
              </table>
            </PagerTemplate>    
          </asp:GridView>
          <div class="btnContainer">
              <div class="btn btn-height_small btn-style_dominant">
                  <asp:LinkButton ID="lbtNewRecord" runat="server" OnClick="lbtNewRecord_Click"><span>Create New Record</span></asp:LinkButton>
              </div>
              <div class="btn btn-height_small btn-style_subtle">
                  <a onclick="openParticipantModal();"><span>Cancel</span></a>
              </div>
          </div>
        </ContentTemplate>
        <Triggers>
          <asp:AsyncPostBackTrigger ControlID="gvSearchResults" EventName="PageIndexChanging" />
          <asp:AsyncPostBackTrigger ControlID="gvSearchResults" EventName="Sorting" />
        </Triggers>
      </asp:UpdatePanel>

In the code behind I have a SetPaging method that is called on the GridView OnDataBound event:

private void SetPaging(GridView gv)
 {
   GridViewRow row = gv.BottomPagerRow;

   var place = row.Cells[0];

   var first = new LinkButton();
   first.CommandName = "Page";
   first.CommandArgument = "First";
   first.Text = "First";
   first.ToolTip = "First Page";

   if (place != null) place.Controls.Add(first);

   var lbl = new Label();
   lbl.Text = " ";
   if (place != null) place.Controls.Add(lbl);

   var prev = new LinkButton();
   prev.CommandName = "Page";
   prev.CommandArgument = "Prev";
   prev.Text = "Prev";
   prev.ToolTip = "Previous Page";

   if (place != null) place.Controls.Add(prev);

   var lbl2 = new Label();
   lbl2.Text = " ";
   if (place != null) place.Controls.Add(lbl2);

   for (int i = 1; i <= gv.PageCount; i++)
   {
     var btn = new LinkButton();
     btn.CommandName = "Page";
     btn.CommandArgument = i.ToString();

     if (i == gv.PageIndex + 1)
     {
       btn.BackColor = Color.Gray;
     }

     btn.Text = i.ToString();
     btn.ToolTip = "Page " + i.ToString();

     if (place != null) place.Controls.Add(btn);

     var lbl3 = new Label();
     lbl3.Text = " ";
     if (place != null) place.Controls.Add(lbl3);
   }

   var next = new LinkButton();
   next.CommandName = "Page";
   next.CommandArgument = "Next";
   next.Text = "Next";
   next.ToolTip = "Next Page";

   if (place != null) place.Controls.Add(next);

   var lbl4 = new Label();
   lbl4.Text = " ";
   if (place != null) place.Controls.Add(lbl4);

   var last = new LinkButton();
   last.CommandName = "Page";
   last.CommandArgument = "Last";
   last.Text = "Last";
   last.ToolTip = "Last Page";

   if (place != null) place.Controls.Add(last);

   var lbl5 = new Label();
   lbl5.Text = " ";
   if (place != null) place.Controls.Add(lbl5);
 }

The paging works if I don't use custom paging, but I really need to use the custom paging. I can't figure out why the PageIndexChanging event isn't fired when I'm using the custom paging.

Thanks,

Jeff

A: 

Well.... I'm not too familiar with custom paging, but... when an event does not fire for a databound control it's generally one of two things:

1) You are re-databinding at the incorrect time. 2) The control heirarchy is not identical on the postback page.

Since you are creating this control dynamically, I'm gonna go with #2 here. In fact you might have a combination of these two problems, since you are creating the controls in the OnDataBound event. That means the only time this control is created is when you call DataBind, which you should not do on postback until after you've handled the events. So you've coded yourself a little Catch-22 here.

Isn't it possible to add a custom pager via markup? That would fix your problem as then you wouldn't be relying on this dynamically created control. Your other option would be to move the dynamic control creation to the Init event.

Bryan
I would prefer to dynamically create the pager, as there are multiple GridViews on the page. I tried moving the call to SetPaging(GridView gv) from the gvSearchResults_DataBound event to the gvSearchResults_Init event, but that causes an exception atvar place = row.Cells[0];because the gv.BottomPagerRow is null.
JeffCren
An update - on a page without the UpdatePanel, the custom paging works, and I have this when I view the source:<ahref="javascript:__doPostBack('ctl00$PlaceHolderBody$gvSearchResults','Page$2')">2</a>On the updatepanel, I get:<ahref="javascript:__doPostBack('ctl00$PlaceHolderModal$gvSearchResults$ctl13$ctl06','')">2</a>It drops the Command and Argument ('Page$2').
JeffCren
Aaah. What, the UpdatePanel is causing a problem? Who would've thought... I'm pretty sure UpdatePanel is responsible for 50% of the ASP.NET questions on StackOverflow. What is ctl13$ctl06? Is that the panel ID?
Bryan
A: 

If you create the GridView dynamically it will save you a lot of hassle : see this link E.g. all you need is a DataSet and the rest will be done by the control ...

YordanGeorgiev