多条件查询并绑定岛GridView实现分页 的问题
现在有一个页面 里面有多个条件查询...然后吧查询结果绑定到GridView中.但是无法分页...
if (!IsPostBack)
{
bind();
}
因为多条件查询的判断是在一个"LinkButton1_Click"事件中
所以现在有点晕不知道怎么吧得到的 SQL字符串传递到bind()方法中了
代码如下:
-------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class byName : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("server=O;uid=sa;database=data");
//string sql;
string sql = "select * from 发热分析 where 1=1";
string num = "";
protected void Page_Load(object sender, EventArgs e)
{
string sql_2 = "select * from 发热分析";
SqlDataAdapter da = new SqlDataAdapter(sql_2, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
//if (!IsPostBack)
//{
// bind();
//}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string rbl_1 = RadioButtonList1.Text;
string rbl_2 = RadioButtonList2.Text;
string rbl_3 = RadioButtonList3.Text;
if (!this.txtSelect.Text.Trim().Equals(""))
{
sql += " and 病名='" + txtSelect.Text + "'";
}
if (this.RadioButtonList1.SelectedIndex != -1)
{
sql += " and 热度='" + rbl_1 + "'";
}
if (this.RadioButtonList2.SelectedIndex != -1)
{
sql += " and 热程='" + rbl_2 + "'";
}
if (this.RadioButtonList3.SelectedIndex != -1)
{
sql += " and 热型='" + rbl_3 + "'";
}
string strValue = this.CheckBoxList1.SelectedValue; ///获得所有被选中的值,
string[] strSingle = strValue.Split(',');///分解字符串,
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected)
{
sql += " and " + this.CheckBoxList1.Items[i].Text + "='1'";
}
}
bind(sql);
}
public void bind(string sql)
{
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataSource = ds;
GridView1.DataBind();
}
//public DataSet Readdate(string sql)
//{
// DataSet ds = new DataSet();
// con.Open();
// SqlDataAdapter da = new SqlDataAdapter(sql, con);
// da.Fill(ds);
// con.Close();
// return ds;
//}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int num = GridView1.PageIndex + 1;
Label1.Text = "当前第" + num + "页/共" + GridView1.PageCount + "页";
}
protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
bind(sql);
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
//选择列
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
num = this.GridView1.SelectedDataKey["id"].ToString();
this.Label2.Text = num;
}
}
-------------------
本人比较菜 希望能说详细一点 .谢谢 --------------------编程问答-------------------- 添加到数据集里面就可以了,然后绑定。
慢慢来
这个是一个基础问题。 --------------------编程问答-------------------- 能不能说出来方法...
我理论比较差...谢谢拉.. --------------------编程问答-------------------- 该回复被版主删除 --------------------编程问答-------------------- 该回复被版主删除 --------------------编程问答-------------------- 你发了个毛呀,靠...
--------------------编程问答-------------------- 我来啦,帮你顶起,声明个全局变量,保存当前页数 --------------------编程问答-------------------- 唉~~~~~~~~~~~~~~~~ --------------------编程问答-------------------- 试着用用ObjectDataSource
--------------------编程问答-------------------- 有个分页的pageDataSource你可以看看,另外如果你没有实现选择不同条件分页有可能是,你请求的时候没有重新绑定。肯定代码逻辑上有问题
补充:.NET技术 , ASP.NET