1 Star 0 Fork 0

luweijun/tosouth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Search.aspx.cs 4.58 KB
一键复制 编辑 原始数据 按行查看 历史
luweijun 提交于 2018-04-28 11:23 . first commit
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace Directory.Web
{
public partial class Search : System.Web.UI.Page
{
protected int pageIndex;
protected int pageSize = WebSettings.GetInstance().SearchItemPageSize;
protected int itemsRecords;
protected string categoryIdKey = "categoryId";
protected string pageKey = "page";
protected string keywordsKey = "key";
protected string searchtypeKey = "searchtype";
protected void Page_Load(object sender, EventArgs e)
{
int searchtype = 0;
int.TryParse(Request.QueryString[searchtypeKey], out searchtype);
if (!IsPostBack)
{
ddlSearchType.Items.Clear();
ddlSearchType.Items.Add(Resources.Resource.Item);
ddlSearchType.Items.Add(Resources.Resource.Category);
ddlSearchType.Items.Add("全部");
ddlSearchType.SelectedIndex = 0;
if (!int.TryParse(Request.QueryString[pageKey], out pageIndex))
{
pageIndex = 0;
}
string keywords = string.Empty;
try
{
keywords = Server.UrlDecode(Request.QueryString[keywordsKey]).Trim();
}
catch
{
ltMsg.Text = "查询字符为空。";
return;
}
if (string.IsNullOrEmpty(keywords))
{
ltMsg.Text = "查询字符为空。";
return;
}
ddlSearchType.SelectedIndex = searchtype;
try
{
if (searchtype == 1 || searchtype == 2)
{
rptCateogrys.DataSource = CategoryDataProxy.SearchCategories(keywords);
rptCateogrys.DataBind();
this.pagebreak.Visible = false;
}
if (searchtype == 0 || searchtype == 2)
{
rptItemsList.DataSource = ItemDataProxy.SearchItems(-999, keywords, pageIndex, pageSize, out itemsRecords);
rptItemsList.DataBind();
this.pagebreak.Visible = true;
SetPageNav();
}
}
catch
{
tbKeywords.Text = "抱歉,出错了。";
return;
}
tbKeywords.Text = keywords;
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
Server.Transfer("Search.aspx?" + keywordsKey + "=" + Server.UrlEncode(tbKeywords.Text) + "&" + searchtypeKey + "=" + ddlSearchType.SelectedIndex);
}
private string GetPageNavigateUrl(int _pageIndex)
{
return "Search.aspx?" + "&" + keywordsKey + "=" + Request.QueryString[keywordsKey] + "&" + pageKey + "=" + _pageIndex.ToString() + "&" + searchtypeKey + "=" + ddlSearchType.SelectedIndex;
}
private void SetPageNav()
{
int pageCounts = ((itemsRecords - 1) / pageSize) + 1;
lbCurrentPage.Text = (pageIndex + 1) + " / " + pageCounts;
if (pageCounts < 1)
{
hplFirstPage.Enabled = false;
hplPrePage.Enabled = false;
hplNextPage.Enabled = false;
hplLastPage.Enabled = false;
}
else if (pageIndex == 0)
{
hplFirstPage.Enabled = false;
hplPrePage.Enabled = false;
if (pageCounts == 1)
{
hplNextPage.Enabled = false;
hplLastPage.Enabled = false;
}
else
{
hplNextPage.Enabled = true;
hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
hplLastPage.Enabled = true;
hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
}
}
else if (pageIndex >= pageCounts - 1)
{
pageIndex = pageCounts - 1;
hplFirstPage.Enabled = true;
hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
hplPrePage.Enabled = true;
hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
hplNextPage.Enabled = false;
hplLastPage.Enabled = false;
}
else
{
hplFirstPage.Enabled = true;
hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
hplPrePage.Enabled = true;
hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
hplNextPage.Enabled = true;
hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
hplLastPage.Enabled = true;
hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
}
}
protected void rptItemsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Directory.Model.ItemInfo item = (Directory.Model.ItemInfo)e.Item.DataItem;
Literal lt = e.Item.FindControl("ltCategoryPath") as Literal;
if (lt != null && item != null)
{
foreach (Directory.Model.CategoryInfo cat in CategoryDataProxy.GetRecursiveParentsOfCategory(item.CategoryId))
{
string link = string.Format("<a href='ItemView.aspx?categoryId={0}'>{1}</a>", cat.CategoryId.ToString(), cat.CategoryName);
lt.Text += "»" + link;
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/luweijun/tosouth.git
git@gitee.com:luweijun/tosouth.git
luweijun
tosouth
tosouth
master

搜索帮助