GridView 在用户控件内时不分页或排序

问题描述 投票:0回答:1

我的 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

任何帮助将不胜感激。

asp.net gridview pagination user-controls gridview-sorting
1个回答
0
投票

很抱歉,我在问题中粘贴的代码虽然其目的是正确的,但并不是与该问题相关的正确代码。也就是说,我要感谢您

Icarus
花时间查看我的代码。

分页/排序有效。我修复此问题的方法是将

GetCampaignData()
PageIndexChanging
事件处理程序中对
Sorting
的调用替换为 SQL 代码和数据绑定方法,以便为我提供所需的结果。

© www.soinside.com 2019 - 2024. All rights reserved.