我试图在 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 中没有显示搜索结果。谁能帮我这个?
谢谢