如何在另一页上显示搜索结果 [英] How display Search Result on Another page

查看:77
本文介绍了如何在另一页上显示搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生M先生在我的房地产网站上有一个用于搜索物业的搜索表格,有很多条件,例如物业类型,城市,地区,价格,面积,我想在差异页面上显示搜索结果,而不是在同一页面上...我" hv编写了一个查询,可以正确地搜索数据库,但是我不知道如何在另一页上显示搜索结果并将其绑定到listview中...在页面加载时...

我这样做是通过存储在会话中的(datatable)dt绑定到searchresult页面上的listview

Sir M having a search form in my real estate site for searching properties there are many criterias like propertytype,city, locality,price,area i want to display the search result on diff page not on the same page...i''hv written a query which searchers the database properly but i dont know how to display the search result on another page and bind them in listview...on page load...

m doing like this to bind listview on searchresult page by (datatable)dt stored in session

SqlConnection con = new SqlConnection(str);
 SqlDataAdapter da = new SqlDataAdapter();
 DataTable dt = new DataTable();
 dt = Session["Search"] as DataTable;
 da.Fill(dt);
 listview1.DataSource = dt;
 listview1.DataBind();


但是显示错误..
填充:SelectCommand.Connection属性尚未初始化.

背后的代码


but it is showing error..
Fill: SelectCommand.Connection property has not been initialized.

Code Behind

if (rbtnfs.Checked == true)
        {
            try
            {
                SqlConnection con = new SqlConnection(str);
                string strQ = ("select S.Property_Type,C.City_Name,L.Locality_Name,S.Price,S.Bedrooms,S.Area From tbl_post_property_sale as S inner join tbl_City as C on C.City_Id=S.City_id inner join tbl_Locality as L on L.Locality_Id=S.Locality_Id where S.Property_type='" + ddlpropertytype.SelectedItem.Text + "'and C.City_Id=" + ddlcity.SelectedValue + " and L.Locality_Id=" + ddllocality.SelectedValue + " and S.price between " + ddlminprice.SelectedValue + " and " + ddlmaxprice.SelectedValue + " and S.Bedrooms=" + ddlbedrooms.SelectedItem.Text + " and S.Area between '" + (txtminarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'and '" + (txtmaxarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'");
                SqlCommand cmdsearch = new SqlCommand(strQ, con);
                con.Open();
                cmdsearch.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {

            }

        }
        else
        {
            try
            {
                SqlConnection con = new SqlConnection(str);
                string strQ = ("select R.Property_Type,C.City_Name,L.Locality_Name,R.Price,R.Bedrooms,R.Area From tbl_post_property_rent as R inner join tbl_City as C on C.City_Id=R.City_id inner join tbl_Locality as L on L.Locality_Id=R.Locality_Id where R.Property_type='" + ddlpropertytype.SelectedItem.Text + "'and C.City_Id=" + ddlcity.SelectedValue + " and L.Locality_Id=" + ddllocality.SelectedValue + " and R.price between " + ddlminprice.SelectedValue + " and " + ddlmaxprice.SelectedValue + " and R.Bedrooms=" + ddlbedrooms.SelectedItem.Text + " and R.Area between '" + (txtminarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'and '" + (txtmaxarea.Text).Trim() + " " + ddlarea.SelectedItem.Text + "'");
                SqlCommand cmdsearch = new SqlCommand(strQ, con);
                con.Open();
                cmdsearch.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {

            }

        }

推荐答案

SqlConnection con = new SqlConnection(str);
 SqlDataAdapter da = new SqlDataAdapter();
 DataTable dt = new DataTable();
 dt = Session["Search"] as DataTable;
 da.Fill(dt);
 listview1.DataSource = dt;
 listview1.DataBind();


实现不正确.

我看到您在Session中已经有数据.既然有了它,就不需要打数据库并取回数据.只需:


Incorrect implementation.

I see you already have data in Session. Since you have it, you don''t need to hit DB and get back data. Simply do:

if(Session["Search"] != null)
{
  listview1.DataSource = (DataTable) Session["Search"];
  listview1.DataBind();
}




补充一下,出现错误的原因是:您没有命令就定义了一个数据适配器,并试图填充数据集.如果要访问数据库并获取数据,请首先定义适配器命令(可能是select语句),然后从中填充数据集.




Just to add on, the reason why you were getting an error was: You defined a dataadapter without a command and tried to fill the dataset. If you want to hit database and get data, first define the adapter command (probably a select statement) and then fill a dataset from it.


存储结果会话并从以下位置获取会话结果其他页面.
store your result session and get result from session on other page .


如果您正在从数据库中检索结果,则只需将select方法的参数传递给查询字符串字典,然后将其返回到要显示的页面中数据输入.

它看起来像这样:domain/folder/showResults.aspx?id1 = this&id2 = that
然后在showResults页面中,您需要做的是:
通过执行以下操作,从查询字符串中获取差异ID:
var id1 = Request.QueryString [id1];等等.
希望对您有所帮助.
I your are retrieving your results from a database, you can just pass the parameters of the select method to the query string dictionary and get them back in the page you want to show data in.

it will look something like this: domain/folder/showResults.aspx?id1=this&id2=that
Then in the showResults page all you need to do is:
get the differents ids from the query string by doing this:
var id1 = Request.QueryString[id1]; and so on.
I hope this to help you.


这篇关于如何在另一页上显示搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆