私は1つの検索ページを持っています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)
}
あります1つの検索ボタンを使用してプロパティを検索できます。 私は、ユーザーがwww.mysite.com/default.aspx?city=1 & state = 2のようなURL全体を直接入力できる機能を実装しました。これにより、ページの読み込み時に検索結果が得られます。
次の検索ボタンをクリックすると、他のユーザーがブラウザに直接入力できる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 正確な検索結果が得られず、代わりに第1検索条件の結果が表示されます。
誰でも間違いを理解できますか?
ページロードイベントに問題はありますか?
または、コードに変更が必要ですか?
このコードは、&は、ページネーションのために、ページあたり15のプロパティ制限し、すべてのプロパティを取り出す:@Chetan Ranpariyaにより示唆されるように
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;
}
}
を、それが働いているが、改ページは、問題を与え始めました。それは55のプロパティをすべて与えますが、2ページ目をクリックすると結果はゼロになり、理想的には次の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と2番目のURLの違いを観察する必要があります。 –
デバッグ後、私はbtnSearch_Clickの後にすべての値が正しいと判断し、mainUrlに割り当てられた値も正常ですが、再びpageloadに進み、URL(最初の問題の検索条件)から値を取り出します。それは問題です – ace
'search_by_country'どのような制御ですか? –