如题,贴上代码,菜鸟,写的很乱,帮忙看看
    protected void Page_Load(object sender, EventArgs e)
    {
        //第一次加载执行,保证点击按钮不执行里面的操作
        if (!Page.IsPostBack)
        {
           
            bind1();
         }
    }    bool flag = false;//如果页面第一次加载,执行bind1()里else代码,如果是点击查询之后,执行if代码    public void bind1()
    {
        if (flag==true)
        {
            string text = TextBox1.Text;
            string time1 = TextBox2.Text;
            string time2 = TextBox3.Text;
            //Response.Write(time1);
            SqlConnection conn = new SqlConnection(strcon);
            string sql = @"SELECT FileName , actiondate , DeviceName,PrimaryOwner FROM dbo.Computer 
                                        AS c join dbo.ShadowCopyAction AS s on c.Computer_Idn = s.Computer_Idn
                                        where DeviceName like N'%" + text + "%' and Actiondate>='" + time1 + "' and ActionDate<='" + time2 + "'or c.PrimaryOwner like N'%" + text + "%' and Actiondate>='" + time1 + "' and ActionDate<='" + time2 + "'";            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);            string sql2 = @"SELECT count(*) FROM dbo.Computer 
                                        AS c join dbo.ShadowCopyAction AS s on c.Computer_Idn = s.Computer_Idn
                                        where DeviceName like N'%" + text + "%' and Actiondate>='" + time1 + "' and ActionDate<='" + time2 + "'or c.PrimaryOwner like N'%" + text + "%' and Actiondate>='" + time1 + "' and ActionDate<='" + time2 + "' ";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql2, conn);
            int a = Convert.ToInt32(cmd.ExecuteScalar());            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            AspNetPager1.RecordCount = a;
            //允许分页  
            pds.AllowPaging = true;
            //当前页索引.
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //Response.Write(pds.CurrentPageIndex );
            //设置每页显示项数
            pds.PageSize = AspNetPager1.PageSize;            Repeater1.DataSource = pds;
            Repeater1.DataBind();
        }
        else
        {
            if (Request["filename"] == null)
            {
                SqlConnection conn = new SqlConnection(strcon);
                string sql = @"SELECT FileName , actiondate , DeviceName,PrimaryOwner FROM dbo.Computer 
                                AS c join dbo.ShadowCopyAction AS s on c.Computer_Idn = s.Computer_Idn
                                ";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = ds.Tables[0].DefaultView;
                AspNetPager1.RecordCount = bl.getshu();
                //允许分页  
                pds.AllowPaging = true;
                //当前页索引.
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //Response.Write(pds.CurrentPageIndex );
                //设置每页显示项数
                pds.PageSize = AspNetPager1.PageSize;                Repeater1.DataSource = pds;
                Repeater1.DataBind();
            }
            else
            {                file = (Request["filename"]).ToString();
                string file2 = file.Trim();
                //Response.Write(file2.Length-file2.IndexOf('\\')-1);
                string file3;                file2 = file2.Replace("?", "%");
                file2 = file2.Replace("?", "%");                SqlConnection conn = new SqlConnection(strcon);
                string sql = @"SELECT filename,actiondate , DeviceName,PrimaryOwner FROM dbo.Computer
                                AS c join dbo.ShadowCopyAction AS s on c.Computer_Idn = s.Computer_Idn
                                where filename like N'%" + file2 + "%'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);                string sql2 = @"SELECT count(*) FROM dbo.Computer
                                AS c join dbo.ShadowCopyAction AS s on c.Computer_Idn = s.Computer_Idn
                                where filename like N'%" + file2 + "%' ";
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql2, conn);
                int a = Convert.ToInt32(cmd.ExecuteScalar());                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = ds.Tables[0].DefaultView;
                AspNetPager1.RecordCount = a;
                //允许分页  
                pds.AllowPaging = true;
                //当前页索引.
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //Response.Write(pds.CurrentPageIndex );
                //设置每页显示项数
                pds.PageSize = AspNetPager1.PageSize;
                Repeater2.DataSource = pds;
                Repeater2.DataBind();
                //Response.Write(file3);
            }
        }
    }    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
      
        bind1();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        flag = true;
        bind1();
    }

解决方案 »

  1.   

    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 manage_users : System.Web.UI.Page
    {
        SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);    private string TableNames = "users";   //表名,可以是多个表,但不能用别名
        private const string PrimaryKey = "id";  //主键,可以为空,但@Order为空时该值不能为空
        private string Fields = "";  //要取出的字段,可以是多个表的字段,可以为空,为空表示select *
        private int PageSize = 20;  //每页记录数
        private int CurrentPage = 0;  //当前页,0表示第1页
        private string Filter = "";  //条件,可以为空,不用填 where
        private string Group = "";   //分组依据,可以为空,不用填 group by
        private string Order = "id desc";  //排序,可以为空,为空默认按主键升序排列,不用填 order by    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                down2_bind();
                ViewState["CurrentPage"] = "0";  //这个变量保存当前页
                totalCount("select count(*) from users");
                bind();
            }
        }
    public void bind()
        {
            //下面是存储过程AspNetPager1分页
            SqlCommand cmd = new SqlCommand("usp_PagingLarge", sqlcon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@TableNames", SqlDbType.VarChar, 200));
            cmd.Parameters.Add(new SqlParameter("@PrimaryKey", SqlDbType.VarChar, 100));
            cmd.Parameters.Add(new SqlParameter("@Fields", SqlDbType.VarChar, 200));
            cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
            cmd.Parameters.Add(new SqlParameter("@CurrentPage", SqlDbType.Int, 4));
            cmd.Parameters.Add(new SqlParameter("@Filter", SqlDbType.VarChar, 200));
            cmd.Parameters.Add(new SqlParameter("@Group", SqlDbType.VarChar, 200));
            cmd.Parameters.Add(new SqlParameter("@Order", SqlDbType.VarChar, 200));
            cmd.Parameters["@TableNames"].Value = TableNames;
            cmd.Parameters["@PrimaryKey"].Value = PrimaryKey;
            cmd.Parameters["@Fields"].Value = Fields;
            cmd.Parameters["@PageSize"].Value = PageSize;
            cmd.Parameters["@CurrentPage"].Value = CurrentPage;
            cmd.Parameters["@Filter"].Value = Filter;
            cmd.Parameters["@Group"].Value = Group;
            cmd.Parameters["@Order"].Value = Order;        SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataKeyNames = new string[] { "id" };//主键
            GridView1.DataBind();
        }
      

  2.   

    下面是查找的    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string searchStr = "1=1";
            if (TextBox1.Text != "")
            {
                searchStr = searchStr + " and name= '"+TextBox1.Text+"'";
            }
            if (TextBox2.Text != "")
            {
                searchStr = searchStr + " and username= '" + TextBox2.Text + "'";
            }
            if (DropDownList1.Text != "")
            {
                searchStr = searchStr + " and sex= '" + DropDownList1.SelectedValue + "'";
            }
            if (DropDownList2.Text != "")
            {
                searchStr = searchStr + " and deptID= '" + DropDownList2.SelectedValue + "'";
            }        totalCount("select count(*) from users where "+searchStr+"");
            Filter = searchStr;
            bind();
        }
      

  3.   

    ViewState["condition"]=searchStr ;