0

在页面加载时,我用记录填充 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
4

1 回答 1

1

快速浏览一下您的代码表明您在页面加载中缺少这一行

 If Not Page.IsPostBack Then
    ->put your code here
 End if

每次页面加载时都会刷新您的网格视图,从而消除您的过滤功能。希望这会有所帮助。

于 2013-02-28T08:22:24.233 回答