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();
        }
    }
  • 相关阅读:
    推荐:《TestDrive ASP.NET MVC》 节选与作者访谈
    30天敏捷结果(15):让自己处于宁静状态
    MDSF:如何使用GMF来做TOGAF建模工具
    强烈推荐:好书、好人、好谚语
    推荐:50个加强表单的jQuery插件
    101与金根回顾敏捷个人:(1)基于MBTI模型发现你的职业性格
    30天敏捷结果(19):你在为谁做事?
    30天敏捷生活(13):获得他人的反馈
    SourceForge.net上的新项目(2005/07/05)
    Open License,开源的许可生成器,实现你自己的许可管理器/应用
  • 原文地址:https://www.cnblogs.com/MySpace/p/1599742.html
Copyright © 2011-2022 走看看