![](/img/trans.png)
[英]ListBox when I select a value from list: why half time that's select 1st value as well with 2nd event?
[英]2nd time click event gives the same result as the 1st click event gives
我有一個搜索頁面說default.aspx,用戶可以在其中搜索屬性。
如果不進行回發,則會在頁面加載時調用以下函數,該函數僅包含3個屬性:
GetDefault();
GetDefault()的語法:
private void GetDefault()
{
DataTable dtProperties = Service.GetDefaultList();
}
如果是回發,則會調用以下函數,該函數將刪除所有屬性:
GetProperties(city, state, country);
{
DataTable dtProperties = service.GetPropertyList(city, state, country)
}
有一個搜索按鈕,使用該按鈕可以搜索屬性。 我已經實現了一些功能,以便用戶可以直接輸入整個網址,例如www.mysite.com/default.aspx?city=1&state=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,州= 2
它給出了准確的搜索結果。 但是,當用戶再次單擊具有不同搜索條件的搜索按鈕時,例如:city = 2,state = 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);
}
問題出在您的Page_Load
代碼上。 當您直接使用city=1&state=2&country=3
瀏覽URL時,它可以正常工作。 但是,當你在下拉列表中的一個變化值,然后單擊該按鈕, PostBack
發生,回發不會改變URL。 這將首先執行Page_Load
。 由於您首先要檢查查詢字符串,因此您仍然可以從查詢字符串中獲得舊值city=1&state=2&country=3
,您還可以通過search_by_city.Value = cityID.ToString();
將其分配給下拉列表search_by_city.Value = cityID.ToString();
。
在Page_Load
之后,將執行按鈕單擊的實際事件處理程序,此時控件的值將更改為舊值,這將使用舊值創建重定向URL,這就是為什么您看到舊搜索結果的原因。
要解決此問題,應將Page_Load
的代碼更改為以下內容。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString.Count == 0)
{
// If it is not post back and no querystring parameters,
// load default properties.
GetDefault();
}
else
{
// If there are any querystring parameters,
// Use them to filer the result and set the control values.
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;
}
GetProperties(city, state, city)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.