Gridview binding is lost when page index changed VB.NET

391 Views Asked by At

I am using Ajax to call a BindGridview() class on code behind to bind the values from database. However, it only works at first time, when i try to change the page index on the gridview, it becomes empty. Below is my code.

I used <%=PostBackString %> to call the function on code behind.

 $.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 %> will jump to my page load to call the BindGridview() class

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

This is my BindGridview() class which will set the data bind to the 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

It works very fine until this part, but my problem is when I try to change the page index, its become empty. I have already set the GridView1_PageIndexChanged and GridView1_PageIndexChanging as below

    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

This is the screenshot of before and after the page index change.

Before

![Before

After click other page After

I would like to know what is the reason this is happening. Can anybody assist me to solve this problem? I really don't know how to solve this issue. Appreciate your help.

0

There are 0 best solutions below