Controls
Controls Web Form Controls HTML Form Controls HTML Server Controls (runat=server) Web Controls Validation Controls Rich Controls List controls HTML Form Controls
特殊屬性AutoPostBack 表示控制項內容或選取項目發生改變時,是否自動發生伺服器的自動回傳
CheckBox & CheckBoxList AutoPostBack=True 當CheckBox有改變時會執行OnCheckedChanged
CheckBoxList 程式動態新增 CheckBoxList1.Items.Add("123"); ListItem li1=new ListItem("item4","4"); DropDownList1.Items.Add(li1); CheckBoxList1.Items.Add(li1);
CheckBoxList if(CheckBoxList1.Items[0].Selected) { 動作 } 把AutoPostBack設定成 True private void CheckBoxList1_SelectedIndexChanged(object sender, System.EventArgs e) { string msg=""; for(int i=0;i<=CheckBoxList1.Items.Count-1;i++) if(CheckBoxList1.Items[i].Selected) msg = msg + "press " + i.ToString(); } Response.Write(msg);
RadioButton & RadioButtonList if(RadioButtonList1.Items[0].Selected) string msg=""; for(int i=0;i<=RadioButtonList1.Items.Count-1;i++) { if(RadioButtonList1.Items[i].Selected) msg = msg + "press " + i.ToString(); } Response.Write(msg);
DropDownList DropDownList1.Items.Clear(); DropDownList1.Items.Add("item1"); DropDownList1.Items.Add("item2"); ListItem li1=new ListItem("item4","4"); DropDownList1.Items.Add(li1);
動態產生Table //新增一列 TableRow newrow=new TableRow(); //新增三欄 TableCell newcell1=new TableCell(); TableCell newcell2=new TableCell(); TableCell newcell3=new TableCell(); newcell1.Text="a"; newcell2.Text="b"; newcell3.Text="c"; // 將欄加入列 newrow.Cells.Add(newcell1); newrow.Cells.Add(newcell2); newrow.Cells.Add(newcell3); //將列併入表格 Table1.Rows.Add(newrow);
Validation Controls
RequiredFieldValidator 驗證對象 錯誤訊息 Submit前檢查要驗證的控制項是否有輸入值
CompareValidator 驗證對象 錯誤訊息 Submit前檢查要驗證的控制項輸入值是否相等
RangeValidator 驗證對象 錯誤訊息 範圍 Submit前檢查要驗證的控制項輸入值是否在範圍內 要注意type的設定
RegularExpressionValidator 驗證對象 錯誤訊息 Submit前檢查要驗證的控制項輸入值是否符合設定的規則
RegularExpressionValidator ValidationExpression 身分證 [A-Z](1|2)\d{8}
DataBind
資料繫結 by control private void Page_Load(object sender, System.EventArgs e) { sqlDataAdapter1.Fill(dataSet11); DropDownList2.DataBind(); }
登入
登入判斷 // 檢查帳密正確性 SqlDataAdapter da=new SqlDataAdapter("select * from teacher where id='" + TextBox1.Text + "' and pwd='" + TextBox2.Text + "'",sqlConnection1); // 把資料從新的Adapter填入(FILL)Dataset DataSet ds2=new DataSet(); da.Fill(ds2,“teacher"); if(ds2.Tables[“teacher"].Rows.Count>0) { Session[“loginname”] = ds2.Tables[“teacher”].Rows[0][“name”]; //登入者姓名 }
資料繫結 by 程式 private void Page_Load(object sender, System.EventArgs e) { // 在這裡放置使用者程式碼以初始化網頁 SqlConnection conn=new SqlConnection("workstation id=MYCHAT-851ACB0C;packet size=4096;user id=sa;data source=MYCHAT-851ACB0C;persist security info=True;initial catalog=Northwind;password=12345"); conn.Open(); SqlCommand cmd=new SqlCommand("select * from employees",conn); SqlDataReader dr=cmd.ExecuteReader(); DropDownList1.DataSource=dr; DropDownList1.DataTextField="LastName"; DropDownList1.DataValueField="EmployeeID"; DropDownList1.DataBind(); dr.Close(); dr=cmd.ExecuteReader(); CheckBoxList1.DataSource=dr; CheckBoxList1.DataTextField="LastName"; CheckBoxList1.DataBind(); } 記得要 using System.Data.SqlClient;