2011-11-22 67 views
1

我的Gridview不會進行頁面排序或排序。通過這個我的意思是,當我嘗試按列進行排序或者當我嘗試通過GridView進行頁面調度時,GridView中的數據不會改變。GridView在用戶控件中不進行分頁或排序

我把它放在用戶控件上的UpdatePanel中(.ascx)。當我在用戶控制之外嘗試時,下面的代碼正常工作。

這裏是我的代碼:

<asp:GridView runat="server" ID="grdStats" AutoGenerateColumns="false" Width="100%" AllowSorting="true" 
     AllowPaging="true" PageSize="20" PagerSettings-Mode="NumericFirstLast" PagerSettings-Position="TopAndBottom"> 
     <Columns> 
      <asp:BoundField DataField="campaignname" HeaderText="Campaign Name" SortExpression="campaignname" /> 
      <asp:BoundField DataField="site_name" HeaderText="Outlet" SortExpression="site_name" /> 
      <asp:BoundField DataField="filename" HeaderText="Media" SortExpression="filename" /> 
      <asp:BoundField DataField="playinterval" HeaderText="Play Interval" SortExpression="playinterval" /> 
     </Columns> 
    </asp:GridView> 

這裏的後端:

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 & " 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 <> 0 Then 
      sql = "SELECT" & vbCrLf & _ 
      " a.campaignid," & vbCrLf & _ 
      " a.companyid," & vbCrLf & _ 
      " a.campaignname," & vbCrLf & _ 
      " a.filename," & vbCrLf & _ 
      " startdate," & vbCrLf & _ 
      " enddate," & vbCrLf & _ 
      " a.playinterval," & vbCrLf & _ 
      " a.ActiveStatus," & vbCrLf & _ 
      " b.*," & vbCrLf & _ 
      " (SELECT COUNT(*)" & vbCrLf & _ 
      "  FROM campaignstats AS c" & vbCrLf & _ 
      "  WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _ 
      "  AND c.campaignid = a.campaignid" & vbCrLf & _ 
      " ) AS today," & vbCrLf & _ 
      " (SELECT COUNT(*)" & vbCrLf & _ 
      "  FROM campaignstats AS d" & vbCrLf & _ 
      "  WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _ 
      "  AND d.campaignid = a.campaignid" & vbCrLf & _ 
      " ) AS past_week," & vbCrLf & _ 
      " (SELECT COUNT(*)" & vbCrLf & _ 
      "  FROM campaignstats AS e" & vbCrLf & _ 
      "  WHERE e.campaignid = a.campaignid" & vbCrLf & _ 
      " ) AS playcount" & vbCrLf & _ 
      "FROM campaigns AS a" & vbCrLf & _ 
      "INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _ 
      "WHERE a.ActiveStatus = 1" & vbCrLf & _ 
      "AND a.companyid = " & CompanyID 

     Else 
      sql = "SELECT" & vbCrLf & _ 
      " a.campaignid," & vbCrLf & _ 
      " a.companyid," & vbCrLf & _ 
      " a.campaignname," & vbCrLf & _ 
      " a.filename," & vbCrLf & _ 
      " startdate," & vbCrLf & _ 
      " enddate," & vbCrLf & _ 
      " a.playinterval," & vbCrLf & _ 
      " a.ActiveStatus," & vbCrLf & _ 
      " b.*," & vbCrLf & _ 
      " (SELECT COUNT(*)" & vbCrLf & _ 
      "  FROM campaignstats AS c" & vbCrLf & _ 
      "  WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _ 
      "  AND c.campaignid = a.campaignid" & vbCrLf & _ 
      " ) AS today," & vbCrLf & _ 
      " (SELECT COUNT(*)" & vbCrLf & _ 
      "  FROM campaignstats AS d" & vbCrLf & _ 
      "  WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _ 
      "  AND d.campaignid = a.campaignid" & vbCrLf & _ 
      " ) AS past_week," & vbCrLf & _ 
      " (SELECT COUNT(*)" & vbCrLf & _ 
      "  FROM campaignstats AS e" & vbCrLf & _ 
      "  WHERE e.campaignid = a.campaignid" & vbCrLf & _ 
      " ) AS playcount" & vbCrLf & _ 
      "FROM campaigns AS a" & vbCrLf & _ 
      "INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _ 
      "WHERE a.ActiveStatus = 1" 


     End If 
    Else 
     ' User is not an admin. 
     sql = "SELECT" & vbCrLf & _ 
     " a.campaignid," & vbCrLf & _ 
     " a.companyid," & vbCrLf & _ 
     " a.campaignname," & vbCrLf & _ 
     " a.filename," & vbCrLf & _ 
     " startdate," & vbCrLf & _ 
     " enddate," & vbCrLf & _ 
     " a.playinterval," & vbCrLf & _ 
     " a.ActiveStatus," & vbCrLf & _ 
     " b.*," & vbCrLf & _ 
     " (SELECT COUNT(*)" & vbCrLf & _ 
     "  FROM campaignstats AS c" & vbCrLf & _ 
     "  WHERE DAY(date_played) = DAY(CURRENT_DATE)" & vbCrLf & _ 
     "  AND c.campaignid = a.campaignid" & vbCrLf & _ 
     " ) AS today," & vbCrLf & _ 
     " (SELECT COUNT(*)" & vbCrLf & _ 
     "  FROM campaignstats AS d" & vbCrLf & _ 
     "  WHERE date_played > CURRENT_DATE - INTERVAL 7 DAY" & vbCrLf & _ 
     "  AND d.campaignid = a.campaignid" & vbCrLf & _ 
     " ) AS past_week," & vbCrLf & _ 
     " (SELECT COUNT(*)" & vbCrLf & _ 
     "  FROM campaignstats AS e" & vbCrLf & _ 
     "  WHERE e.campaignid = a.campaignid" & vbCrLf & _ 
     " ) AS playcount" & vbCrLf & _ 
     "FROM campaigns AS a" & vbCrLf & _ 
     "INNER JOIN companies AS b ON a.companyid = b.companyid" & vbCrLf & _ 
     "WHERE a.ActiveStatus = 1" & vbCrLf & _ 
     "AND a.companyid = " & 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 

任何幫助將不勝感激。

+0

你的代碼對我來說很不錯。你有沒有其他的代碼在usercontrol後面? – Icarus

+0

只需編碼並從另一個網格中刪除信息即可。這個應該是隻讀的。如果有幫助,當我填充這個網格時(通過點擊另一個網格上的選擇按鈕),調試器在去其他任何事情之前打兩次Page_Load。 – Ortund

回答

0

我很抱歉我在這個問題中粘貼的代碼,雖然正確的目的,不是正確的代碼與這個問題有關。這就是說,我想感謝您Icarus花時間查看我的代碼。

分頁/排序工作。我所做的修復是將PageIndexChangingSorting事件處理程序中的GetCampaignData()替換爲所需的SQL代碼和數據綁定方法,以便爲我提供所需的結果。

相關問題