新手问一个关于GridView带查询条件分页的显示问题,大家帮帮忙

rookie926 2008-03-11 01:53:09
aspx页是个带条件的查询,结果分页显示,刚载入页面时全部数据分页显示各页跳转都没问题,选择了条件再查,第1页的数据没问题,但点第2页时显示的却是不按条件查时第2页显示的数据,再点第1页时第1页也变成不带条件查询时第一页的数据了,查询条件不起作用了。。
<asp:GridView ID="GridView1" AllowPaging="True" runat="server" AutoGenerateColumns="False" CellSpacing="0" CellPadding="0" Width="572px" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem li1 = new ListItem("请选择", "请选择");
DropDownList1.Items.Add(li1);
DropDownList2.Items.Add(li1);
for (int i = 1; i < 5; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList1.Items.Add(li);
}
DropDownList1.DataBind();
DropDownList2.Items.Add(new ListItem("使用", "1"));
DropDownList2.Items.Add(new ListItem("未使用", "0"));
DropDownList2.DataBind();
}
...//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void Button1_Click(object sender, ImageClickEventArgs e)
{
string state = DropDownList2.SelectedValue;
string jb = DropDownList1.SelectedValue;
string code = TextBox2.Text;
string name = TextBox3.Text;
if (state.Equals("请选择"))
{
state = "";
}
if (jb.Equals("请选择"))
{
jb = "";
}
...//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}

第一次发提问贴,大家帮帮忙
...全文
411 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lushuibin 2010-08-16
你解决的,你把怎么解决的说下啊!
回复
c304189466 2008-11-18

protected void btnAdd_Click(object sender, EventArgs e)
{
SqlData da = new SqlData();
string cmdtxt = "insert into PeoplesCon(" + this.DropDownList1.SelectedValue + ") values('" + this.txtAdd.Text + "') ";
if (da.ExceSQL(cmdtxt))
{
Response.Write("<script language=javascript>alert('操作成功!')</script>");

}
else
{
Response.Write("<script language=javascript>alert('操作失败!')</script>");
}

}
回复
rookie926 2008-03-11
问题解决了,谢谢大家
回复
rookie926 2008-03-11
把那几句放到if里去了,又有新问题了,查询后第一页还是正常显示,但点分页后整个gridview控件就消失了,这是为什么啊?
回复
正宗老冉 2008-03-11

以下语句要放到 if (!IsPostBack) {} 体内,
...//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind();
回复
而且改下你的代码
protected void Page_Load(object sender, EventArgs e) 
{
if (!IsPostBack)
{
ListItem li1 = new ListItem("请选择", "请选择");
DropDownList1.Items.Add(li1);
DropDownList2.Items.Add(li1);
for (int i = 1; i < 5; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList1.Items.Add(li);
}
DropDownList1.DataBind();
DropDownList2.Items.Add(new ListItem("使用", "1"));
DropDownList2.Items.Add(new ListItem("未使用", "0"));
DropDownList2.DataBind();
//===============第一次绑定放这里
//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
/*...//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind(); */
}
回复

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem li1 = new ListItem("请选择", "请选择");
DropDownList1.Items.Add(li1);
DropDownList2.Items.Add(li1);
for (int i = 1; i < 5; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList1.Items.Add(li);
}
DropDownList1.DataBind();
DropDownList2.Items.Add(new ListItem("使用", "1"));
DropDownList2.Items.Add(new ListItem("未使用", "0"));
DropDownList2.DataBind();
}
...//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
改成
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem li1 = new ListItem("请选择", "请选择");
DropDownList1.Items.Add(li1);
DropDownList2.Items.Add(li1);
for (int i = 1; i < 5; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList1.Items.Add(li);
}
DropDownList1.DataBind();
DropDownList2.Items.Add(new ListItem("使用", "1"));
DropDownList2.Items.Add(new ListItem("未使用", "0"));
DropDownList2.DataBind();

...//定义的str_sql
DataTable dt = DataCommSQLServer.GetSqlData(str_sql);
GridView1.DataSource = dt;
GridView1.DataBind();

}
}



因爲每次postback都會跑Page_Load裏的不在if (!IsPostBack) ()代嗎
回复
没禁用viewstate吧??
应该是你选择的条件没保存下来.
回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2008-03-11 01:53
社区公告

让您成为最强悍的C#开发者