default.aspx.cs

来自「水晶报表详细资料水晶报表详细资料水晶报表详细资料」· CS 代码 · 共 93 行

CS
93
字号
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;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page 
{  
    protected string sqlStr ;
    public static  int i;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            i = 0;
            sqlStr = "select * from tb_stu";  
            GVBind(sqlStr);
        }
           
    }
    //连接数据库
    protected SqlConnection GetConnetion()
    {
        string conn = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(conn);
        return myConn;
    }
    //DataView的绑定
    protected void   GVBind(string sqlStr)
    {
        SqlConnection myConn = GetConnetion();
        myConn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlStr,myConn );
        DataSet ds = new DataSet();
        da.Fill(ds,"student");
        GridView1.DataSource = ds.Tables["student"].DefaultView;
        GridView1.DataBind();
    }
    //查询时,DataView的绑定
    protected void SelectBind()
    {
        if (TextBox1.Text == null)

            sqlStr = "select * from tb_stu";
        else if (validateCode(TextBox1.Text))
        {
           sqlStr = "select * from tb_stu where 年龄>'" + TextBox1.Text + "'";
       
        }
        else
        {  Response.Write("<script>alert('年龄输入有误,请输入大小零的实数')</script>");
           sqlStr = "select * from tb_stu";
        
        }
              GVBind(sqlStr);
    }

    //查询按钮
    protected void Button1_Click(object sender, EventArgs e)
    {
        i = 1;
        SelectBind();
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        if (i == 1)
        { 
        
        SelectBind();
        
        }
        else
        {
            sqlStr = "select * from tb_stu";
            GVBind(sqlStr);
        
        }
       
    }
    public bool validateCode(string str)
    {
        return Regex.IsMatch(str,"^\\d+$");
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?