zoukankan      html  css  js  c++  java
  • GridView 和 ViewState 来实现条件查寻

    using System;
    using System.Data;
    using System.Configuration;
    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 _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                bind();
            }
        }
        string sql = "select * from Tree";
       public void  bind()
        {
            //Response.Write(ViewState["text"].ToString());
            if(ViewState["text"]==null||ViewState["text"].ToString()=="")
            {
                sql = "select * from Tree";
            }
            else
            {
                sql += " where "+this.DropDownList1.SelectedValue+" like'%"+ViewState["text"].ToString()+"%'";
            }
            this.GridView1.DataSource=Query(sql);
            this.GridView1.DataBind();
        }
        public DataTable Query(string sql)
        {
            SqlConnection sqlcon = new SqlConnection("server=LUOBO-77FC1CCCD\\FRANK;database=TreeDB;uid=sa;pwd=sasa;");
            SqlDataAdapter sqladap=new SqlDataAdapter (sql,sqlcon);
            //SqlDataReader sqlreader=new SqlDataReader();
            DataSet ds=new DataSet ();
            sqladap.Fill(ds);
            return ds.Tables[0];      
        }

        
        protected void  GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex=e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex =-1;
            bind();
        }
        //条件查询:
        protected void Button1_Click(object sender,EventArgs e)
        {
            //if(this.TextBox1.Text.Trim() != "")
            //{
                ViewState["text"] = this.TextBox1.Text.Trim();
            //}
            bind();
        }
    }
  • 相关阅读:
    降龙十八掌之三:(见龙在田)优化查询性能
    完整的项目工程目录结构
    降龙十八掌之一:(亢龙有悔)SQL Server Profiler和数据库引擎优化顾问
    ASP.NET状态管理的总结
    LINQ 图解
    获取IP城市
    Eclipse 编译StanfordNLP
    Centos JAVA Eclipse
    关掉PUTTY后,进程仍可以运行。
    centos lnmp 安装笔记
  • 原文地址:https://www.cnblogs.com/MySpace/p/1599742.html
Copyright © 2011-2022 走看看