I have a GridView that shows particular data and records when I click Generate button.
My problem is that when I click for the next page of the GridView list the Gridview will be lost, I don't think the data will be lost too, I guess the GirdView just doesn't show it.
Can you please help me with this problem. I'm not the one who first create this website and I tried all the things that I know for this problem, I also searched for a solutions in the internet with no luck. So I hope someone can tell me why is the GridView not showing after clicking the next page.
Here's the code for the Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)
V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")
If Not Page.IsPostBack Then
V1Division.DataSource = GetDivision()
V1Division.DataBind()
V1BR.Items.Clear()
V1BR.DataSource = GetBusinessRule(V1Division.Text)
V1BR.DataBind()
MultiView1.ActiveViewIndex = 0
Panel1.DefaultButton = "V1Generate"
End If
End Sub
Here's the code for GridView1_PageIndexChanging
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
GridView1.DataSource = SortTable(dt)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
Here's the code for BindGridView
Private Sub BindGridview(ByVal query As String, ByVal countquery As String)
ViewState.Add("Query", query)
ViewState.Add("CountQuery", countquery)
Dim count As Integer = 0
If Description.Text = "1" Then
ValidateFamily(query)
Else
DataSource = dbHelper.GetRecordDT(query)
End If
count = DataSource.Rows.Count
'GridView1.DataSource = Nothing
'GridView1.DataBind()
'GridView1.PageIndex = 0
GridView1.DataSource = DataSource
GridView1.Visible = True
GridView1.DataBind()
ExportToExcel.Visible = IIf(count < 1, False, True)
Panel1.Visible = IIf(count < 1, False, True)
SetTotalResult(count)
End Sub
I also tried to put this code in Page_Load
If ViewState.Item("Query") <> Nothing Then
BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
End If
But the result is a problem also, it shows the GridView data list when I click the next page but when clicking the Generate button it's like this
or like this
Please help me with this. Thank you so much.
UPDATE
Okay so I put this code in Page_Load
If ViewState.Item("Query") <> Nothing Then
BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
End If
Then I put GridView1.Visible = True
to GridView1_PageIndexChanging
and when I click the next page it shows No Record Found only even if it fetched the data already.
So I've been doing this thing for a while now and thankfully I did it already. It took me a lot of different approach and solutions that didn't help me at all but thankfully, this one is correct now.
Code for Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)
V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")
If Not Page.IsPostBack Then
V1Division.DataSource = GetDivision()
V1Division.DataBind()
V1BR.Items.Clear()
V1BR.DataSource = GetBusinessRule(V1Division.Text)
V1BR.DataBind()
MultiView1.ActiveViewIndex = 0
Panel1.DefaultButton = "V1Generate"
If ViewState.Item("Query") <> Nothing Then
BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
End If
End If
End Sub
Code for GridView1_PageIndexChanging
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
GridView1.DataSource = SortTable(dt)
GridView1.PageIndex = e.NewPageIndex
'GridView1.DataBind()
If ViewState.Item("Query") <> Nothing Then
BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
End If
End Sub
I add
If ViewState.Item("Query") <> Nothing Then
BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
End If
in If Not Page.IsPOstBack
for showing the record when selecting again in the DropDownList and in PageIndexChanging
for showing record when clicking for the next page.