0
我想在asp.net中的同一個搜索頁上的gridview上顯示搜索結果。這裏是UI:Gridview不在asp.net中查看結果
這裏是我的代碼:
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控件。誰能幫我這個?
感謝
工作,謝謝。但是我不明白爲什麼它現在能夠工作,我的代碼和你的解決方案有什麼區別? – yrazlik
@bigO'Response.Redirect'會再次加載您的頁面。你會失去搜索選擇等。 – Damith