我有一個搜索頁面說的Default.aspx 在那裏,用戶可搜索的屬性。第2次點擊事件給出了相同的結果作爲第1點擊事件給
在頁面加載,如果它不回發下面的函數獲取調用,它僅取出3個屬性:
GetDefault();
語法GetDefault():
private void GetDefault()
{
DataTable dtProperties = Service.GetDefaultList();
}
,如果它的後背部下面的函數被取消所有屬性:
GetProperties(city, state, country);
{
DataTable dtProperties = service.GetPropertyList(city, state, country)
}
有一個搜索按鈕,使用它可以搜索該屬性。 我已經實現的功能,使得用戶可以直接輸入整個URL像www.mysite.com/default.aspx?city=1 &狀態= 2,這將給他搜索頁面加載結果。
在搜索按鈕上單擊下面的函數生成URL,其他任何用戶都可以直接放在他/她的瀏覽器&上得到相應的搜索結果。
protected void btnSearch_Click(object sender, EventArgs e)
{
//TO build URL
string RootUrl = WebConfigurationManager.AppSettings["WEBROOT"];
String target_url = "";
if ((Request.Form["search_by_city"])!= "")
{
string cityName = service.getCityByID(Int32.Parse(search_by_city.Value)).ToString();
target_url += "&city=" + cityName;
}
else
{
city = 0;
}
if ((Request.Form["search_by_state"]) != "")
{
string stateName= service.getStateNameByID(Int32.Parse(search_by_state.Value)).ToString();
target_url += "&state=" + stateName;
}
else
{
state = 0;
}
if ((Request.Form["search_by_country"]) != "")
{
string countryName = service.getCountryNameByID(Int32.Parse(search_by_country.Value)).ToString();
target_url += "&country=" + countryName;
}
else
{
country= 0;
}
string mainUrl = RootUrl + "/default.aspx?" + target_url;
Response.Redirect(mainUrl);
}
此代碼根據提交的表單值生成URL。
頁面加載事件如下:當用戶進行搜索首次
protected void Page_Load(object sender, EventArgs e)
{
//To decode filters
//To get city
if (Request.QueryString["city"] != null)
{
string aCityName = Request.QueryString["city"].ToString();
cityID = service.getCityByName(aCityName);
search_by_city.Value = cityID.ToString();
}
else
{
cityID = 0;
}
//To get state
if (Request.QueryString["state"] != null)
{
string aState = Request.QueryString["state"].ToString();
state = service.getStateByName(aState);
search_by_state.Value = state.ToString();
}
else
{
state= 0;
}
//To get country
if (Request.QueryString["country"] != null)
{
string aCountry = Request.QueryString["country"].ToString();
country = DLResale.getConfigByName(aCountry);
search_by_country.Value = country.ToString();
}
else
{
country= 0;
}
if (Request.QueryString.Count == 0)
{
if (!IsPostBack)
{
GetDefault();
}
else
{
GetProperties(city, state, country);
}
}
else
{
GetProperties(city, state, city)
}
}
這所有的代碼工作完全正常。
說,城市= 1,國家第1次用戶搜索= 2
它給出了精確的搜索結果。但是,當用戶在搜索按鈕再次點擊不同的搜索條件,說:城市= 2,狀態= 2 它沒有給出確切的搜索,而是給出了結果的第一個搜索條件!
任何人都可以找出任何錯誤?
有在頁面加載事件的任何問題?
或者任何改變都需要在代碼?????
這段代碼取出所有屬性&限制每頁15個屬性分頁的目的:
private void GetProperties(int city, int state, int country)
{
DataTable dtProperties = service.GetPropertyList(city, state, country);
if (dtProperties.Rows.Count > 0)
{
last = dtProperties.Rows.Count - 1;
PagedDataSource pageds = new PagedDataSource();
DataView dv = new DataView(dtProperties);
pageds.DataSource = dv;
pageds.AllowPaging = true;
pageds.PageSize = 15;
if (ViewState["PageNumber"] != null)
{
pageds.CurrentPageIndex = Convert.ToInt32(ViewState["PageNumber"]);
}
else
{
pageds.CurrentPageIndex = 0;
}
if (pageds.PageCount > 1)
{
rptPaging2.Visible = true;
rptPaging.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pageds.PageCount; i++)
pages.Add((i + 1).ToString());
rptPaging.DataSource = pages;
rptPaging.DataBind();
}
else
{
rptPaging.Visible = false;
rptPaging2.Visible = false;
//ErrorNoPropertiesDiv.Visible = true;
}
rptProperties.DataSource = pageds;
rptProperties.DataBind();
}
else
{
ErrorNoPropertiesDiv.Visible = true;
rptProperties.Visible = false;
rptPaging2.Visible = false;
}
}
由@Chetan Ranpariya建議,它的工作,但分頁開始給的問題。它提供了所有55個屬性,但是當我點擊第二頁時,它會給出零結果,理想情況下它應該顯示接下來的15個屬性。
它處理分頁的代碼如下:
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
protected void rptPaging_ItemDataBound(object source, RepeaterItemEventArgs e)
{
LinkButton lnk = (LinkButton)e.Item.FindControl("lnkPage");
if (lnk.CommandArgument.ToString() == (PageNumber + 1).ToString())
{
lnk.BackColor = System.Drawing.Color.Black;
}
}
protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument) - 1;
GetProperties(city, state, country);
}
你調試了代碼嗎?特別是'btnSearch_Click'代碼,並檢查從'search_by_state.Value'和其他控件獲取的值以及在重定向之前爲'mainUrl'分配的值。 「search_by_state」,「search_by_city」和「search_by_country」是什麼類型的控件?您需要觀察第一個URL和第二個URL之間的區別。 –
調試後,我發現,在btnSearch_Click提交的所有值都正確之後,分配給mainUrl的值也很好,但是它又一次轉到頁面加載並從URL(第一個問題的搜索條件)中取出值。這是問題 – ace
'search_by_country'它是什麼樣的控制? –