title: GridView isn't paging or sorting when inside a user control
body: <p>My Gridview won't page or sort. By this I mean that the data in the GridView doesn't change when I try to sort by a column or when I try to page through the GridView.</p>
  
  <p>I have it inside an UpdatePanel on a user control (<code>.ascx</code>). The code below works fine when I try it outside of the user control.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>    &lt;asp:GridView runat="server" ID="grdStats" AutoGenerateColumns="false" Width="100%" AllowSorting="true"
          AllowPaging="true" PageSize="20" PagerSettings-Mode="NumericFirstLast" PagerSettings-Position="TopAndBottom"&gt;
          &lt;Columns&gt;
              &lt;asp:BoundField DataField="campaignname" HeaderText="Campaign Name" SortExpression="campaignname" /&gt;
              &lt;asp:BoundField DataField="site_name" HeaderText="Outlet" SortExpression="site_name" /&gt;
              &lt;asp:BoundField DataField="filename" HeaderText="Media" SortExpression="filename" /&gt;
              &lt;asp:BoundField DataField="playinterval" HeaderText="Play Interval" SortExpression="playinterval" /&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Here's the backend:</p>
  
  <pre><code>Protected Sub grdStats_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdStats.PageIndexChanging
      grdStats.PageIndex = e.NewPageIndex
      GetCampaignData()
  End Sub
  
  Protected Sub grdStats_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdStats.Sorting
  
      If e.SortExpression = ViewState("sortExpr") Then
          ViewState("sortExpr") = e.SortExpression &amp; " desc"
      Else
          ViewState("sortExpr") = e.SortExpression
      End If
  
      GetCampaignData()
  
  End Sub
  
  ' ######## POPULATE DATA #########
  
  Private Sub GetCampaignData(Optional ByVal CompanyID As Integer = 0)
      Dim sql As String = Nothing
  
      ' Check user access level.
      If Session("userlevel") = 1 Then
          ' Current user is admin.
  
          ' If a company has been filtered, get it's data, else, get all campaign data.
          If CompanyID &lt;&gt; 0 Then
              sql = "SELECT" &amp; vbCrLf &amp; _
              "   a.campaignid," &amp; vbCrLf &amp; _
              "   a.companyid," &amp; vbCrLf &amp; _
              "   a.campaignname," &amp; vbCrLf &amp; _
              "   a.filename," &amp; vbCrLf &amp; _
              "   startdate," &amp; vbCrLf &amp; _
              "   enddate," &amp; vbCrLf &amp; _
              "   a.playinterval," &amp; vbCrLf &amp; _
              "   a.ActiveStatus," &amp; vbCrLf &amp; _
              "   b.*," &amp; vbCrLf &amp; _
              "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
              "       FROM campaignstats AS c" &amp; vbCrLf &amp; _
              "       WHERE DAY(date_played) = DAY(CURRENT_DATE)" &amp; vbCrLf &amp; _
              "       AND c.campaignid = a.campaignid" &amp; vbCrLf &amp; _
              "   ) AS today," &amp; vbCrLf &amp; _
              "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
              "       FROM campaignstats AS d" &amp; vbCrLf &amp; _
              "       WHERE date_played &gt; CURRENT_DATE - INTERVAL 7 DAY" &amp; vbCrLf &amp; _
              "       AND d.campaignid = a.campaignid" &amp; vbCrLf &amp; _
              "   ) AS past_week," &amp; vbCrLf &amp; _
              "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
              "       FROM campaignstats AS e" &amp; vbCrLf &amp; _
              "       WHERE e.campaignid = a.campaignid" &amp; vbCrLf &amp; _
              "   ) AS playcount" &amp; vbCrLf &amp; _
              "FROM campaigns AS a" &amp; vbCrLf &amp; _
              "INNER JOIN companies AS b ON a.companyid = b.companyid" &amp; vbCrLf &amp; _
              "WHERE a.ActiveStatus = 1" &amp; vbCrLf &amp; _
              "AND a.companyid = " &amp; CompanyID
  
          Else
              sql = "SELECT" &amp; vbCrLf &amp; _
              "   a.campaignid," &amp; vbCrLf &amp; _
              "   a.companyid," &amp; vbCrLf &amp; _
              "   a.campaignname," &amp; vbCrLf &amp; _
              "   a.filename," &amp; vbCrLf &amp; _
              "   startdate," &amp; vbCrLf &amp; _
              "   enddate," &amp; vbCrLf &amp; _
              "   a.playinterval," &amp; vbCrLf &amp; _
              "   a.ActiveStatus," &amp; vbCrLf &amp; _
              "   b.*," &amp; vbCrLf &amp; _
              "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
              "       FROM campaignstats AS c" &amp; vbCrLf &amp; _
              "       WHERE DAY(date_played) = DAY(CURRENT_DATE)" &amp; vbCrLf &amp; _
              "       AND c.campaignid = a.campaignid" &amp; vbCrLf &amp; _
              "   ) AS today," &amp; vbCrLf &amp; _
              "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
              "       FROM campaignstats AS d" &amp; vbCrLf &amp; _
              "       WHERE date_played &gt; CURRENT_DATE - INTERVAL 7 DAY" &amp; vbCrLf &amp; _
              "       AND d.campaignid = a.campaignid" &amp; vbCrLf &amp; _
              "   ) AS past_week," &amp; vbCrLf &amp; _
              "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
              "       FROM campaignstats AS e" &amp; vbCrLf &amp; _
              "       WHERE e.campaignid = a.campaignid" &amp; vbCrLf &amp; _
              "   ) AS playcount" &amp; vbCrLf &amp; _
              "FROM campaigns AS a" &amp; vbCrLf &amp; _
              "INNER JOIN companies AS b ON a.companyid = b.companyid" &amp; vbCrLf &amp; _
              "WHERE a.ActiveStatus = 1"
  
  
          End If
      Else
          ' User is not an admin. 
          sql = "SELECT" &amp; vbCrLf &amp; _
          "   a.campaignid," &amp; vbCrLf &amp; _
          "   a.companyid," &amp; vbCrLf &amp; _
          "   a.campaignname," &amp; vbCrLf &amp; _
          "   a.filename," &amp; vbCrLf &amp; _
          "   startdate," &amp; vbCrLf &amp; _
          "   enddate," &amp; vbCrLf &amp; _
          "   a.playinterval," &amp; vbCrLf &amp; _
          "   a.ActiveStatus," &amp; vbCrLf &amp; _
          "   b.*," &amp; vbCrLf &amp; _
          "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
          "       FROM campaignstats AS c" &amp; vbCrLf &amp; _
          "       WHERE DAY(date_played) = DAY(CURRENT_DATE)" &amp; vbCrLf &amp; _
          "       AND c.campaignid = a.campaignid" &amp; vbCrLf &amp; _
          "   ) AS today," &amp; vbCrLf &amp; _
          "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
          "       FROM campaignstats AS d" &amp; vbCrLf &amp; _
          "       WHERE date_played &gt; CURRENT_DATE - INTERVAL 7 DAY" &amp; vbCrLf &amp; _
          "       AND d.campaignid = a.campaignid" &amp; vbCrLf &amp; _
          "   ) AS past_week," &amp; vbCrLf &amp; _
          "   (SELECT COUNT(*)" &amp; vbCrLf &amp; _
          "       FROM campaignstats AS e" &amp; vbCrLf &amp; _
          "       WHERE e.campaignid = a.campaignid" &amp; vbCrLf &amp; _
          "   ) AS playcount" &amp; vbCrLf &amp; _
          "FROM campaigns AS a" &amp; vbCrLf &amp; _
          "INNER JOIN companies AS b ON a.companyid = b.companyid" &amp; vbCrLf &amp; _
          "WHERE a.ActiveStatus = 1" &amp; vbCrLf &amp; _
          "AND a.companyid = " &amp; CInt(Session("companyid"))
      End If
  
      grdCampaigns.DataSource = get_data(sql)
      grdCampaigns.DataBind()
  
      For i As Integer = 0 To grdCampaigns.Rows.Count - 1
          grdCampaigns.Rows(i).Cells(2).Text = Microsoft.VisualBasic.Left(grdCampaigns.Rows(i).Cells(2).Text, 10)
          grdCampaigns.Rows(i).Cells(3).Text = Microsoft.VisualBasic.Left(grdCampaigns.Rows(i).Cells(3).Text, 10)
      Next
  End Sub
  
  Public Function get_data(ByVal SQLStatement As String) As DataView
  
      'Populates the datatable
      Dim dt As Data.DataTable
  
      dt = db.GetDataTable(SQLStatement)
  
      Dim dv As System.Data.DataView = New System.Data.DataView(dt)
  
      If Not ViewState("sortExpr") Is Nothing Then
          dv.Sort = ViewState("sortExpr")
      Else
          dv = dt.DefaultView
      End If
  
      Session("dv5") = dv
      Return dv
  End Function
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>