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();
        }
相关文章
相关标签/搜索