多条件查询下,GridView的分页问题

zhuzhengwei 2010-12-08 02:40:23
我做了一个多条件查询,结果都在页面的一个GridView上显示。
但是运行后查询结果正常,但是点下页就没数据了,要再点击一下查询,才会显示第二页的数据
我怀疑是分页后数据重新绑定的问题,麻烦各位路过的老师解答一下啊,感激不敬,迅速结贴。
下面我贴出代码:
首先是ASP页面的:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style="height:160px;text-align:center">
<br /><br /><span style="font-size:xx-large;">种群动态信息查询 </span>
<br />
模糊查询: 
<asp:DropDownList ID="DDLCondition" runat="server" Width="120px"
style="margin-left: 0px">
<asp:ListItem Selected="True">查询条件</asp:ListItem>
<asp:ListItem>地点</asp:ListItem>
<asp:ListItem>调查单位名称</asp:ListItem>
</asp:DropDownList>
  关键字:
<asp:TextBox ID="txtCharacter" runat="server" Width="120px"></asp:TextBox>

<span> 
<asp:Button ID="btquery1" runat="server" onclick="btquery1_Click" Text="确定"
Width="46px" />
 

</span>
<br />
起始时间:<asp:TextBox ID="txtStart" runat="server" Height="24px" Width="80px"></asp:TextBox>
<cc1:CalendarExtender ID="txtStart_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtStart">
</cc1:CalendarExtender>
   终止时间: 
<asp:TextBox ID="txtEnd" runat="server" Height="24px" Width="80px"></asp:TextBox>
<cc1:CalendarExtender ID="txtEnd_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtEnd">
</cc1:CalendarExtender>
  
<asp:Button ID="btQuery" runat="server" Height="24px" Text="查询" Width="60px"
onclick="btQuery_Click" />
</div>
<div style="overflow: scroll; height: 700px;width:780px; vertical-align:top; ">
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging">

</asp:GridView>
<asp:label id="lblPageCount" runat="server"></asp:label> 
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton> 
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton> 
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton> 
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
<br />
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
</div>

</asp:Content>

源代码:
public partial class Ui_DataView_PopulationDynamics_check : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

//if (!Page.IsPostBack)
//{
// ViewState["sortColumn"] = "";
// ViewState["sortDirection"] = "";


//}
////分页选择;
//btnFirst.Text = "首页";
//btnPrev.Text = "上一页";
//btnNext.Text = "下一页";
//btnLast.Text = "尾页";
//GridView1.DataBind();
//ShowPageChangedStatus();
}

// 实现按时间段查询的查询及其控制;当没输入起始和截止时间时,默认为查询全部记录;

protected void btQuery_Click(object sender, EventArgs e)
{

if (txtStart.Text.Trim() == "" && txtEnd.Text.Trim() == "")
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["theconnection"].ConnectionString);

SqlCommand cmd = new SqlCommand("DT_PopulationDynamics_Sel_View", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];//引用强数据集中的DataTable;
GridView1.DataBind();
}
else if (txtStart.Text.Trim() != "" && txtEnd.Text.Trim() == "")
{
//PestDataAccess db = new PestDataAccess();
PestDataAccess.Message("提示:请输入终止时间!", "警告");
}
else if (txtStart.Text.Trim() == "" && txtEnd.Text.Trim() != "")
{
PestDataAccess.Message("提示:请输入起始时间!", "警告");
}
else
{
string sql = "";
sql = "SELECT (由于版面,省略数据) WHERE SurveyUnit.FillFormTime>='" + txtStart.Text.Trim() + "' AND SurveyUnit.FillFormTime<='" + txtEnd.Text.Trim() + "'";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["theconnection"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];//引用强数据集中的DataTable;
GridView1.DataBind();
if (GridView1.Rows.Count <= 0)
{
//没有搜索到记录;
PestDataAccess.Message("提示:没有查询到匹配记录!", "提示");
}
conn.Close();

}
}

// 实现按照地点或者调查单位的模糊查询;
protected void btquery1_Click(object sender, EventArgs e)
{
if (DDLCondition.Text.Trim() == "查询条件")
{
PestDataAccess.Message("提示:请选择查询条件!", "警告");
}
else
{
if (DDLCondition.Text.Trim() == "地点")
{
if (txtCharacter.Text.Trim() == "")
{
//关键字为空则提出警告;
PestDataAccess.Message("提示:请输入关键字!", "警告");
}
else
{
string sql = "SELECT (由于版面,省略数据) WHERE Site.SiteName like '%" + txtCharacter.Text.Trim() + "%'";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["theconnection"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];//引用强数据集中的DataTable;
GridView1.DataBind();
if (GridView1.Rows.Count <= 0)
{
//没有搜索到记录;
PestDataAccess.Message("提示:没有匹配记录!", "提示");
}
conn.Close();
}
}
else if (DDLCondition.Text.Trim() == "调查单位名称")
{
if (txtCharacter.Text.Trim() == "")
{
//关键字为空则提出警告;
PestDataAccess.Message("提示:请输入关键字!", "警告");
}
else
{
string sql = "SELECT (由于版面,省略数据)WHERE SurveyUnit.UnitName like '%" + txtCharacter.Text.Trim() + "%'";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["theconnection"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];//引用强数据集中的DataTable;
GridView1.DataBind();

if (GridView1.Rows.Count <= 0)
{
//没有搜索到记录;
PestDataAccess.Message("提示:没有匹配记录!", "提示");
}
conn.Close();
}
}
}
}

/// <summary>
/// 触发页面分页事件;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex;
startIndex = GridView1.PageIndex * GridView1.PageSize;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
ShowPageChangedStatus();
}

/// <summary>
/// 分页按钮事件;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (GridView1.PageIndex < (GridView1.PageCount - 1))
{
GridView1.PageIndex += 1;
}
break;
case "prev":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
case "last":
GridView1.PageIndex = (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex = System.Convert.ToInt32(arg);
break;
}
GridView1.DataBind();
ShowPageChangedStatus();
}

private void ShowPageChangedStatus()
{
lblCurrentIndex.Text = "第"+ (GridView1.PageIndex + 1).ToString() + "页";
lblPageCount.Text = "总共"+ GridView1.PageCount.ToString()+ "页";
}
}
...全文
459 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzhen_927116 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 louti 的回复:]
你分页的时候没有给gridview绑定
你把 查询数据的地方 单独写一个方法比如说叫gridBind() 在PagerButtonClick里把GridView1.DataBind();换成gridBind() 就可以了
[/Quote]+1
zhuzhengwei 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 whb147 的回复:]

重新邦定条件
[/Quote]
怎么处理这个公共绑定问题是关键,用ViewState可以吗?你能不能给个例子啊?
zhuzhengwei 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 louti 的回复:]

你分页的时候没有给gridview绑定
你把 查询数据的地方 单独写一个方法比如说叫gridBind() 在PagerButtonClick里把GridView1.DataBind();换成gridBind() 就可以了
[/Quote]

我的查询条件比较多,语句有很多,是写一个处理SQL语句的方法?
用ViewState麽?
您能给个例子吗
xlj_2008 2010-12-08
  • 打赏
  • 举报
回复
把查询出的结果重新绑定下
majic2008 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lijinsheng2010 的回复:]
protected void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (GridView1.PageIndex < (Gri……
[/Quote]

你只DataBind(),但是没绑定数据源啊
wjy217213 2010-12-08
  • 打赏
  • 举报
回复
给gridview绑定
fendouaj 2010-12-08
  • 打赏
  • 举报
回复
将绑定数据部分提取出来单独写成一个方法例如:GridViewDataBing();
当点击分页的时候传入你的查询之前的查询条件以及页码等,然后重新调用这个方法。
就可以了。
lijinsheng2010 2010-12-08
  • 打赏
  • 举报
回复
protected void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (GridView1.PageIndex < (GridView1.PageCount - 1))
{
GridView1.PageIndex += 1;
}
break;
case "prev":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
case "last":
GridView1.PageIndex = (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex = System.Convert.ToInt32(arg);
break;
}
GridView1.DataBind();
ShowPageChangedStatus();
}

这一段没有绑定数据源,你试着写一个公共的绑定数据的方法,然后在事件中调用,然后看看。。
whb147 2010-12-08
  • 打赏
  • 举报
回复
重新邦定条件
myhope88 2010-12-08
  • 打赏
  • 举报
回复
点击下一页肯定要重新绑定一下数据啊,而且还得把条件带进去,不然结果会不一样的
TimZhuFaith 2010-12-08
  • 打赏
  • 举报
回复
断点执行查看下。。
wwfgu00ing 2010-12-08
  • 打赏
  • 举报
回复
louti 2010-12-08
  • 打赏
  • 举报
回复
你分页的时候没有给gridview绑定
你把 查询数据的地方 单独写一个方法比如说叫gridBind() 在PagerButtonClick里把GridView1.DataBind();换成gridBind() 就可以了

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧