0

我试图在 asp.net 的同一搜索页面上的 gridview 上显示搜索结果。这是用户界面:

在此处输入图像描述

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    searchResults.DataBind();

}
protected void BClassSearch_Click(object sender, EventArgs e)
{
  // if (!IsPostBack)
    //{
        SqlConnection con = new SqlConnection();
        con.ConnectionString = Userfunctions.GetConnectionString();
        con.Open();
        string selected = lbCourseListBox.SelectedValue;

       if(selected!="" && Tcoursenumber.Text!="")
       {
           string query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName] from CourseTable where CourseCode='" + lbCourseListBox.SelectedValue+"' and CourseNumber = '" + Tcoursenumber.Text+"'";

            SqlDataAdapter adap = new SqlDataAdapter(query, con);

            DataTable tab = new DataTable();

            adap.Fill(tab);

            searchResults.DataSource = tab;
            searchResults.DataBind();
       }
       else if (selected != "" && Tcoursenumber.Text == "")
       {
           string query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName] from CourseTable where CourseCode='" + lbCourseListBox.SelectedValue;

           SqlDataAdapter adap = new SqlDataAdapter(query, con);

           DataTable tab = new DataTable();

           adap.Fill(tab);

           searchResults.DataSource = tab;
           searchResults.DataBind();
       }
       else if (selected == "" && Tcoursenumber.Text != "")
       {
           string query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName] from CourseTable where CourseNumber='" + Tcoursenumber.Text;

           SqlDataAdapter adap = new SqlDataAdapter(query, con);

           DataTable tab = new DataTable();

           adap.Fill(tab);

           searchResults.DataSource = tab;
           searchResults.DataBind();
       }
    //}
    Response.Redirect("SearchCourse.aspx");
}

问题是,gridview 中没有显示搜索结果。谁能帮我这个?

谢谢

4

1 回答 1

1

Response.Redirect("SearchCourse.aspx");在按钮单击结束时删除

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // do you want to bind something in first time load? then load it here 
        // searchResults.DataSource = tab;
        searchResults.DataBind();

     }

}

您不想调用 Response.Redirect同一页面,因为当您单击按钮时,它将回发页面。如果您调用Response.Redirect它将加载新页面,您将丢失页面中的所有控件状态。

于 2013-06-02T09:02:29.330 回答