如下图一个搜索区域,AspNetPager分页控件用来实现分页,都知道普通的分页,翻页的时候,在绑定加载数据的方法就可以了。
现在苦闷的是搜索条件太多导致在翻页事件后 不知道该绑定哪个数据加载方法了,像图片所示搜索是随意组合的,那就是条件有N种,现在翻页事件里根本不知道怎么写了,
请教一种实现方式,或哪位做过类似的,分享下。。~~~~~~~
现在苦闷的是搜索条件太多导致在翻页事件后 不知道该绑定哪个数据加载方法了,像图片所示搜索是随意组合的,那就是条件有N种,现在翻页事件里根本不知道怎么写了,
请教一种实现方式,或哪位做过类似的,分享下。。~~~~~~~
//数据库连接字符串
private string conString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; protected void Page_Load(object sender, EventArgs e)
{
//加载公告栏中所有公告内容
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
string strSQL = "select count(*) from MB_NoticePublic";
using (SqlCommand cmd = new SqlCommand(strSQL, con))
{
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
}
}
BindData();
}
} //数据绑定
public void BindData()
{
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
string strSQL = "select * from MB_NoticePublic order by c_dt desc";
SqlDataAdapter da = new SqlDataAdapter(strSQL,con);
DataSet ds = new DataSet();
da.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"tab");
this.GridView1.DataSource = ds.Tables["tab"];
this.GridView1.DataBind();
}
} //翻页事件
protected void AspNetPager1_PageChanged(object src, PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData();
}
以前写个貌似的 用Sql拼接实现 写的有点复杂 做了蛮多判断的 但返回的还是一个dateset
不知道和分页有什么关系?
兄台,我指的就是这里
//翻页事件
protected void AspNetPager1_PageChanged(object src, PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData();-->我搜的条件有30种左右,你能确定这里绑哪个?
}
dropdownlist 每个 是单一搜索
3个文本框是个组合搜索
时间是单个搜索
所有的条件都是查询出一个LIST集合 然后通过这个方法来搜索
private void fenye(int pagesize, int pageIndex, List<quest> list)
{
PagedDataSource pd = new PagedDataSource();
pd.AllowPaging = true;
pd.PageSize = pagesize;
if (pageIndex > 1)
{
pageIndex = 1;
}
pd.CurrentPageIndex = pageIndex - 1;
pd.DataSource = list;
this.anp.RecordCount = list.Count;
this.GridView1.DataSource = pd;
this.GridView1.DataBind();
BindIMG();
}下面是部分搜索代码 中间3个文本框和页面加载时的代码,看了下面可能都知道了页面上还不只一种类型的权限。有的搜索要禁用调,有的不禁掉,所以每换一种用户 搜索的内容多变性就多一倍
// 内部搜索
private void Serach(string mkid, string fbid, string istate)
{
fenye(anp.PageSize, anp.CurrentPageIndex, (List<quest>)questBLL.GYSerach(mkid, fbid, istate));
}
// 用户搜索
private void UserSerach(string mkid, string istate, int fabuid)
{
fenye(anp.PageSize, anp.CurrentPageIndex, (List<quest>)questBLL.KHSerach(mkid, istate, userBLL.GetUserByName(Session["login"].ToString()).ID));
}
//管理员加载内容
private void BindAdminGridView()
{
fenye(anp.PageSize, anp.CurrentPageIndex, questBLL.GetAllQuest());
}
//客户加载内容
private void BindUserGridView()
{
fenye(anp.PageSize, anp.CurrentPageIndex, questBLL.GetAllQuest(userBLL.GetUserByName(Session["login"].ToString()).ID));
}
//组合搜索
protected void linSearch_Click(object sender, EventArgs e)
{
if (userBLL.GetUserByName(Session["login"].ToString()).State == 2)
{
fenye(anp.PageSize, anp.CurrentPageIndex, (List<quest>)questBLL.QuestSearchByKH(this.txtwenti.Text.Trim(), this.txtSSZ.Text.Trim(), userBLL.GetUserByName(Session["login"].ToString()).ID));
}
else
{
fenye(anp.PageSize, anp.CurrentPageIndex, (List<quest>)questBLL.QuestSearchByAdmin(this.txtwenti.Text.Trim(), this.txtKHName.Text.Trim(), this.txtSSZ.Text.Trim()));
}
}
//开始时间段搜索
protected void linTimeSearch_Click(object sender, EventArgs e)
{
fenye(anp.PageSize, anp.CurrentPageIndex, questBLL.QuestSearchByKSTime(txtXQStartTime.Value, this.txtXQEndTime.Value));
}
//回复完成时间段搜索
protected void linHFSearch_Click(object sender, EventArgs e)
{
fenye(anp.PageSize, anp.CurrentPageIndex, questBLL.QuestSearchByWCTime(this.txtHFStartTime.Value, this.txtHFEndTime.Value));
}
部分底层搜索代码/// <summary>
/// 答应客户完成的时间,内部查询用,同一时间模糊查,不同时间时间段查
/// </summary>
public List<quest> QuestSearchByWCTime(string start, string end)
{
if (start == end)
{
return dbhelpler.XQW.ExecuteQuery<quest>("select * from quest where convert(varchar,dywctime,120) like '%" + start + "%'").ToList();
}
else
{
return dbhelpler.XQW.ExecuteQuery<quest>("select * from quest where dywctime between '" + start + "' and '" + end + "'").ToList();
}
}
/// <summary>
/// 内部查询用,组合搜索,根据参数确定要返回的搜索内容
/// </summary>
/// <param name="wenti">问题</param>
/// <param name="name">客户</param>
/// <param name="shishizhe">实施者</param>
/// <returns>LIST集合</returns>
public object QuestSearchByAdmin(string wenti, string name, string shishizhe)
{
var q = from u in dbhelpler.XQW.quest
select u;
if (name == string.Empty && shishizhe == string.Empty) { q = q.Where(p => p.WenTi.Contains(wenti)); } else if (wenti == string.Empty && shishizhe == string.Empty) { q = q.Where(p => p.USER.Name.Contains(name)); } else if (wenti == string.Empty && name == string.Empty) { q = q.Where(p => p.SHISHIZHE.Contains(shishizhe)); } else if (shishizhe == string.Empty) { q = q.Where(p => p.WenTi.Contains(wenti) && p.USER.Name.Contains(name)); } else if (name == string.Empty) { q = q.Where(p => p.WenTi.Contains(wenti) && p.SHISHIZHE.Contains(shishizhe)); } else if (wenti == string.Empty) { q = q.Where(p => p.USER.Name.Contains(name) && p.SHISHIZHE.Contains(shishizhe)); } else { q = q.Where(p => p.WenTi.Contains(wenti) && p.USER.Name.Contains(name) && p.SHISHIZHE.Contains(shishizhe)); } return q.ToList();
}
2 最好只有一个dataset 就不会这么乱了
传入参数
然后用 StringBuilder拼凑
如 :
StringBuilder sql=new StringBuilder("select * from table1 where 1=1");
sql.append(传入参数1==""?"":" and 字段1 = 传入参数1");//页面输入值有这个值时加上此条件
有多少个条件就加多少条这个语句
然后加上你的分页语句
就OK啦
关键点击翻页时我搜的条件有几十种,怎么确定这里要绑哪个?