我gridview用了动态绑定,主要是多条件改变查询语句,那这样的话要怎么绑定分页?
string sql = "SELECT * FROM auto_news where 1=1"; protected void btnss_Click(object sender, EventArgs e)
{
if (this.txttimestar.Text != "" && this.txttimeover.Text != "")
{
DateTime timestar = Convert.ToDateTime(this.txttimestar.Text);
DateTime timeover = Convert.ToDateTime(this.txttimeover.Text);
int i = DateTime.Compare(timestar, timeover);
if (DateTime.Compare(timestar, timeover) == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('开始时间不能大于结束时间,请重选!');location.href='newsAdmin.aspx';</script>");
}
}
string aa = sql;
if (this.txtss.Text.Trim() != "")
aa += " and newsLong like" + "'%" + txtss.Text.Trim() + "%'";
if (txtusername.Text.Trim() != "")
aa += " and userid=(select id from auto_Users where userName=" + "'" + txtusername.Text.Trim() + "'" + ")";
if (txttimestar.Text != "")
aa += " and creatTime>=" + "'" + txttimestar.Text + "'";
if (txttimeover.Text != "")
aa += " and creatTime<=" + "'" + txttimeover.Text + "'";
if (Convert.ToInt32(this.drpclass.SelectedValue) > 1)
aa += " and [auto_news].pid=" + this.drpclass.SelectedValue;
if (this.drpposition.SelectedIndex > 0)
aa += " and charindex(" + "'" + this.drpposition.SelectedValue + "'" + " ,htmlposition)>0";
if (this.drppd.SelectedIndex > 0)
aa += " and [auto_news].cid=" + drppd.SelectedValue;
if (this.drpzt.SelectedIndex > 0)
aa += " and [auto_news].sid=" + drpzt.SelectedValue; gridband(aa);
} protected void lbtysh_Click(object sender, EventArgs e)
{
String aa = sql + " and [auto_news].ifPost=1";
gridband(aa);
} protected void gridband(string sql)
{
List<Autonew> an = AutonewManager.GetAllAutonewsListBySQL(sql);
grvnews.DataSource = an;
grvnews.DataBind();
} protected void grvnews_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvw = (GridView)sender;
if (e.NewPageIndex < 0)
{
TextBox pageNum = (TextBox)gvw.BottomPagerRow.FindControl("txtNewPageIndex");
int Pa = int.Parse(pageNum.Text);
if (Pa <= 0)
{
gvw.PageIndex = 0;
}
else
{
gvw.PageIndex = Pa - 1;
}
}
else
{
gvw.PageIndex = e.NewPageIndex;
}
gvw.DataBind();
}这样写的话点下一页是没有值。。求高手帮忙解决下。谢谢了
string sql = "SELECT * FROM auto_news where 1=1"; protected void btnss_Click(object sender, EventArgs e)
{
if (this.txttimestar.Text != "" && this.txttimeover.Text != "")
{
DateTime timestar = Convert.ToDateTime(this.txttimestar.Text);
DateTime timeover = Convert.ToDateTime(this.txttimeover.Text);
int i = DateTime.Compare(timestar, timeover);
if (DateTime.Compare(timestar, timeover) == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('开始时间不能大于结束时间,请重选!');location.href='newsAdmin.aspx';</script>");
}
}
string aa = sql;
if (this.txtss.Text.Trim() != "")
aa += " and newsLong like" + "'%" + txtss.Text.Trim() + "%'";
if (txtusername.Text.Trim() != "")
aa += " and userid=(select id from auto_Users where userName=" + "'" + txtusername.Text.Trim() + "'" + ")";
if (txttimestar.Text != "")
aa += " and creatTime>=" + "'" + txttimestar.Text + "'";
if (txttimeover.Text != "")
aa += " and creatTime<=" + "'" + txttimeover.Text + "'";
if (Convert.ToInt32(this.drpclass.SelectedValue) > 1)
aa += " and [auto_news].pid=" + this.drpclass.SelectedValue;
if (this.drpposition.SelectedIndex > 0)
aa += " and charindex(" + "'" + this.drpposition.SelectedValue + "'" + " ,htmlposition)>0";
if (this.drppd.SelectedIndex > 0)
aa += " and [auto_news].cid=" + drppd.SelectedValue;
if (this.drpzt.SelectedIndex > 0)
aa += " and [auto_news].sid=" + drpzt.SelectedValue; gridband(aa);
} protected void lbtysh_Click(object sender, EventArgs e)
{
String aa = sql + " and [auto_news].ifPost=1";
gridband(aa);
} protected void gridband(string sql)
{
List<Autonew> an = AutonewManager.GetAllAutonewsListBySQL(sql);
grvnews.DataSource = an;
grvnews.DataBind();
} protected void grvnews_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvw = (GridView)sender;
if (e.NewPageIndex < 0)
{
TextBox pageNum = (TextBox)gvw.BottomPagerRow.FindControl("txtNewPageIndex");
int Pa = int.Parse(pageNum.Text);
if (Pa <= 0)
{
gvw.PageIndex = 0;
}
else
{
gvw.PageIndex = Pa - 1;
}
}
else
{
gvw.PageIndex = e.NewPageIndex;
}
gvw.DataBind();
}这样写的话点下一页是没有值。。求高手帮忙解决下。谢谢了
{
webTitle.Text = new MyShare().GetWebTitle(); //验证Session
if (!new MyShare().CheckSession(SovellModel.Common.Config.OverTimeString, SovellModel.Common.Config.DefaultLoginPageForMerch, SovellModel.Common.Config.SessionIDForMerch)) return; if (!IsPostBack)
{
ViewState["pageIndex"] = 1;
ViewState["pageSize"] = 15; //每页显示15条数据
BindInfo();
} lbtnSearch.Attributes["onclick"] = "javascript:return CheckSettleSearch(" + txtStartTime.ClientID + "," + txtEndTime.ClientID + ")";
}
/// <summary>
/// 地址栏ID加密
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
//public string CryptoID(object obj)
//{
// return EncryptDecrypt.DesEncrypt(obj.ToString(), "");
//}
/// <summary>
/// 前往搜索页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnGO_Click(object sender, EventArgs e)
{
if (this.txtPageIndex.Text == string.Empty) return;
if (Convert.ToInt32(txtPageIndex.Text.Trim()) > Convert.ToInt32(ViewState["pCount"]) || Convert.ToInt32(txtPageIndex.Text.Trim()) <= 0)
{
return;
}
Pager = Convert.ToInt32(txtPageIndex.Text.Trim());
BindInfo();
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFirst_Click(object sender, EventArgs e)
{
Pager = 1;
BindInfo();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnPrev_Click(object sender, EventArgs e)
{
--Pager;
BindInfo();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnNext_Click(object sender, EventArgs e)
{
++Pager;
BindInfo();
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnLast_Click(object sender, EventArgs e)
{
Pager = Convert.ToInt32(ViewState["pCount"]);
BindInfo();
}
#region Private Methods
/// <summary>
/// 绑定订单信息
/// </summary>
private void BindInfo()
{
SearchOrderMethod();
SetNavigateBtnEnable();
this.ltCount.Text = Convert.ToString(ViewState["rCount"]);
this.ltPage.Text = ViewState["pageIndex"] + "/" + ViewState["pCount"];
}
/// <summary>
/// 查询订单
/// </summary>
private void SearchOrderMethod()
{
//验证Session
if (!new MyShare().CheckSession(UpdatePanel1, SovellModel.Common.Config.OverTimeString, SovellModel.Common.Config.DefaultLoginPageForMerch, SovellModel.Common.Config.SessionIDForMerch)) return; int merchid = Convert.ToInt32(Session[SovellModel.Common.Config.SessionIDForMerch]); string no = null;
string starttime = null;
string endtime = null;
no = txtNo.Text.Trim();
if (txtEndTime.Text.Trim() != "")
endtime = txtEndTime.Text.Trim();
if (txtStartTime.Text.Trim() != "")
starttime = txtStartTime.Text.Trim();
int pageCount = 0;
int recordCount = 0;
IList<SH_Settle> settle = SettleBLL.GetSettleInfoByCondition(merchid, no, starttime, endtime, Pager, PagerSize, ref pageCount, ref recordCount);
ViewState["pCount"] = pageCount;
ViewState["rCount"] = recordCount;
this.rpt.DataSource = settle;
this.rpt.DataBind();
this.ltCount.Text = Convert.ToString(ViewState["rCount"]);
this.ltPage.Text = ViewState["pageIndex"] + "/" + ViewState["pCount"];
}
/// <summary>
/// 设置页数
/// </summary>
private int Pager
{
get { return Convert.ToInt32(ViewState["pageIndex"]); }
set { ViewState["pageIndex"] = value; }
}
/// <summary>
/// 设置每页显示记录的数量
/// </summary>
private int PagerSize
{
get { return Convert.ToInt32(ViewState["pageSize"]); }
set { ViewState["pageSize"] = value; }
}
/// <summary>
/// 设置导航键的Enable
/// </summary>
private void SetNavigateBtnEnable()
{
lbtnFirst.Enabled = lbtnLast.Enabled = lbtnPrev.Enabled = lbtnNext.Enabled = true;
int pIndex = Convert.ToInt32(ViewState["pageIndex"]);
int pCount = Convert.ToInt32(ViewState["pCount"]);
int rCount = Convert.ToInt32(ViewState["rCount"]);
if (Convert.ToInt32(ViewState["pCount"]) == 1)
{
lbtnFirst.Enabled = lbtnPrev.Enabled = lbtnLast.Enabled = lbtnNext.Enabled = false;
}
else if (pIndex == 1)
{
lbtnFirst.Enabled = lbtnPrev.Enabled = false;
}
else if (pIndex == pCount)
{
lbtnLast.Enabled = lbtnNext.Enabled = false;
}
}
#endregion
/// <summary>
/// 搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnSearch_Click(object sender, EventArgs e)
{
Pager = 1;
SearchOrderMethod();
SetNavigateBtnEnable();
}
int pageIndex=当前页索引
int pageSize=每页显示的条数
假设list为你的数据源
var pageList=list.Skip((pageIndex-1)*pageSize).Take(pageSize);
然后里面可以
this.gvXX.PageIndex = e.NewPageIndex;
dataBind();如果是分页控件。。那你随意~
</asp:GridView>
cs代码中写GridView1_PageIndexChanging事件
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = 数据绑定;
GridView1.DataBind();
}