在页面加载时,我使用记录填充GridView。然后我创建了一个搜索功能来搜索记录。 GridView实际上是根据搜索文本进行更新的。问题是当我选择一行时,GridView显示所有记录而不是基于搜索的列表。
以下是代码:
Imports MySql.Data.MySqlClient
Public Class Main_Admin
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQL As String
'Dim iRecordCount As Integer
myConnection = New MySqlConnection("server=localhost; user id=root; password=admin; database=db; pooling=false;")
strSQL = "SELECT emp_id AS 'Employee ID', emp_sname AS Surname, emp_fname AS 'First Name', emp_mname AS 'Middle Name', tbl_emp_cat.cat_name AS 'Category', nature AS 'Nature of Employment' FROM tbl_employee JOIN tbl_emp_cat ON tbl_employee.emp_cat = tbl_emp_cat.emp_cat;"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New DataSet()
myDataAdapter.Fill(myDataSet, "mytable")
GridView1.DataSource = myDataSet
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
e.Row.Cells(0).Width = 100
e.Row.Cells(0).Attributes.CssStyle("text-align") = "center"
e.Row.Cells(1).Width = 100
e.Row.Cells(2).Width = 100
e.Row.Cells(3).Width = 100
e.Row.Cells(4).Width = 100
If (e.Row.RowType = DataControlRowType.DataRow) Then
'e.Row.Attributes("onmouseover") = "this.style.cursor='hand';"
e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';"
e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & Convert.ToString(e.Row.RowIndex))
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.textDecoration='underline';")
'e.Row.Attributes.Add("onmouseout", "this.style.textDecoration='none';")
'e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.GridView1, Page.ClientScript.GetPostBackEventReference(GridView1, e.Row.RowIndex)))
' "Select$" + e.Row.RowIndex.ToString())
End If
End Sub
Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
GridView1.SelectedIndex = -1
End Sub
Private Sub search_Click(sender As Object, e As EventArgs) Handles search.Click
Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQL As String
'Dim iRecordCount As Integer
myConnection = New MySqlConnection("server=localhost; user id=root; password=admin; database=db; pooling=false;")
strSQL = "SELECT emp_id AS 'Employee ID', emp_sname AS Surname, emp_fname AS 'First Name', emp_mname AS 'Middle Name', tbl_emp_cat.cat_name AS 'Category', nature AS 'Nature of Employment' FROM tbl_employee JOIN tbl_emp_cat ON tbl_employee.emp_cat = tbl_emp_cat.emp_cat WHERE emp_sname LIKE '%" & search_text.Text + "%';"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New DataSet()
myDataAdapter.Fill(myDataSet, "mytable")
GridView1.DataSource = myDataSet
GridView1.DataBind()
myConnection.Close()
End Sub
End Class
答案 0 :(得分:1)
快速浏览一下代码就会显示您在页面加载中缺少此行
If Not Page.IsPostBack Then
->put your code here
End if
每次页面加载时,您的网格视图都会刷新,从而擦除您的过滤功能。希望这会有所帮助。