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();
    }
}

转载于:https://www.cnblogs.com/MySpace/archive/2009/11/10/1599742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值