当页面索引改变VB.NET时,Gridview绑定会丢失

时间:2016-05-10 02:09:52

标签: asp.net ajax vb.net gridview page-index-changed

我正在使用Ajax在后面的代码上调用BindGridview()类来绑定数据库中的值。但是,它只在第一次工作时,当我尝试更改gridview上的页面索引时,它变为空。以下是我的代码。

我使用<%=PostBackString %>来调用后面代码的函数。

 $.ajax({
                         type: "POST",
                         contentType: "application/json; charset=utf-8",
                         url: "RepairStatusChart.aspx/BindRecord",
                         data: data,
                         dataType: "json",
                         success: function (result) {
                             <%=PostBackString %>
                             alert("Success");
                         },
                         error: function (xhr, textStatus, errorThrown) {
                          alert("Sorry, an unexpected error has occured.");
                         }
                     });

<%=PostBackString %>会跳转到我的页面加载以调用BindGridview()

PostBackString = Page.ClientScript.GetPostBackEventReference(Me, "MyEventArgumentName")
        If IsPostBack Then
            If Request("__EVENTARGUMENT") = "MyEventArgumentName" Then
                BindGridview()
            End If
        End If

这是我的BindGridview()类,它将数据绑定设置为gridview

Protected Sub BindGridview()
        Dim partnum, ddlMonthFrom, ddlYearFrom, ddlMonthTo, ddlYearTo, repaircenter, finalstatus As String

        partnum = HttpContext.Current.Session("partnum").ToString()

        ddlMonthFrom = HttpContext.Current.Session("ddlMonthFrom").ToString()

        ddlYearFrom = HttpContext.Current.Session("ddlYearFrom").ToString()

        ddlMonthTo = HttpContext.Current.Session("ddlMonthTo").ToString()

        ddlYearTo = HttpContext.Current.Session("ddlYearTo").ToString()

        repaircenter = HttpContext.Current.Session("rc")

        finalstatus = HttpContext.Current.Session("status")

        Dim strConnString As String = ConfigurationManager.ConnectionStrings("prcConnectionString").ConnectionString
        Dim con As New MySqlConnection(strConnString)
        Dim select1 As String = "select CountFrequency, SRNo, RefurbishedSN, ShipmentDate, PartsNo, EmployeeID, Alias, Company, PartSN, StartRepairDate, DateReceived,  DateCreated, LastUpdated, EstimatedRepairCompletionDate, Status,datediff(curdate(),STR_TO_DATE(replace(DateReceived,',','-'),'%d-%M-%Y')) as Aging from testvsrparts WHERE (STR_TO_DATE(shipmentdate,'%d, %M, %Y') Between STR_TO_DATE('01, " + ddlMonthFrom.ToString + ", " + ddlYearFrom.ToString + "','%d, %M, %Y') AND STR_TO_DATE('31, " + ddlMonthTo.ToString + ", " + ddlYearTo.ToString + "','%d, %M, %Y')) " + partnum + " AND  Alias ='" + repaircenter.ToString + "' AND  Status ='" + finalstatus.ToString + "'  ;"

        Dim dt As New DataTable()


        dt = conn.query(select1)
        GridView1.DataSource = dt
        GridView1.DataBind()

    End Sub

直到这一部分才能正常工作,但我的问题是当我尝试更改页面索引时,它变为空。我已将GridView1_PageIndexChangedGridView1_PageIndexChanging设置为如下

    Protected Sub GridView1_PageIndexChanged(sender As Object, e As EventArgs) Handles GridView1.PageIndexChanged
                BindGridview()
    End Sub

    Protected Sub GridView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
                GridView1.PageIndex = e.NewPageIndex
                BindGridview()
    End Sub

这是页面索引更改之前和之后的屏幕截图。

之前

![Before

点击其他页面后 After

我想知道这是怎么回事。任何人都可以协助我解决这个问题吗?我真的不知道如何解决这个问题。感谢您的帮助。

0 个答案:

没有答案