ASP.NET知识点小结1

1每个页面都要对session进行判断,这样会很麻烦,可以把判断写成一个网页,然后每个网页都去调用这个网页。

if (Session["flag"] == null || (string)Session["flag"] != "ok")
            {
                Response.Redirect("loginFail.aspx");
            }
Server.Execute("WebForm2.aspx");//调用了上面这个网页。

2在网页里面弹框

Response.Write("<script language='javascript'>alert('Hello World')</script>");

3连接数据库

  public class DB
    {
       
        public static SqlConnection createConnection()
        {
            SqlConnection conn = new SqlConnection("server=.;database=department;uid=sa;pwd=123456;");
            return conn;
        }
    }

4动态给panel加控件

            int txtNum;
            int btnNum;
            txtNum = Convert.ToInt32(TextBox1.Text);
            btnNum = Convert.ToInt32(TextBox2.Text);
            for (int i = 0; i < txtNum; i++)
            {
                Panel1.Controls.Add(new TextBox());
            }
            for (int j = 0; j < btnNum; j++)
            {
                Panel1.Controls.Add(new Button());
            }

5两级下拉框联动

 

   protected void Page_Load(object sender, EventArgs e)
        {
                
            if (!IsPostBack)  //只有第一次加载这个页面才执行这些语句。
            {
            SqlConnection conn = DB.createConnection(); //连接数据库
            conn.Open();
            string sql = "select * from province";//查询省
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataReader reader = cmd.ExecuteReader();
            DropDownList1.DataSource = reader;  //数据源就是reader的表
            DropDownList1.DataTextField = "proName";  //显示的内容
            DropDownList1.DataValueField = "proID";   //没有显示的内容,其实就是主键,把它绑定一下,以后对主键操作会方便
            DropDownList1.DataBind();  //每次绑定数据都要用到这个方法,用来生成相应的HTML语句。
            reader.Close();//关闭SQLdatareader
            string sqlCity = "select * from city where proID="+DropDownList1.SelectedValue.ToString();//根据省查询市

            SqlCommand cmdCity = new SqlCommand(sqlCity,conn);
            reader = cmdCity.ExecuteReader();
            DropDownList2.DataSource = reader;
            DropDownList2.DataTextField = "cityName";
            DropDownList2.DataValueField = "cityID";
            DropDownList2.DataBind();
            reader.Close();
            conn.Close();
            }
        }

//在执行此操作是,先把该 DropDownList1的autopostback的属性设为true,这样才能出发这个SelectedIndexChanged的事件。
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string proID = DropDownList1.SelectedValue.ToString();//选择的下拉框的value值,其实就是主键。这样就动态的跟具现在的省来查询市了。
            SqlConnection conn = DB.createConnection();
            conn.Open();
            string sql = "select * from city where proID="+proID;
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataReader read=cmd.ExecuteReader();
            DropDownList2.DataSource = read;
            DropDownList2.DataTextField = "cityName";
            DropDownList2.DataValueField = "cityID";
            DropDownList2.DataBind();
            read.Close();
        }





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值