(一)複合控件html
1.RadioButtonList 每一行數據都是一個ListItem數據庫
RadioButtonList的屬性:post
RepeatDirection --- 排列方式ui
RepeatColumns --- 每一行中須要展現的個數url
RepeatLayout --- 頁面中生成什麼樣的代碼spa
ListItem的屬性: code
Enable --- 是否啓用orm
selected --- 是否被選中server
Text --- 顯示的文本 xml
Value ---隱藏的值,給系統看的
2. RadioButtonList綁定數據:
綁定數據有兩種方式:
(1)、DataSource
(2)遍歷建立ListItem
取值:
void Button1_Click(object sender, EventArgs e) { Label1.Text= RadioButtonList1.SelectedItem.Text//顯示的內容
+RadioButtonList1.SelectedItem.Value;//給數據庫看的內容 }
3.CheckBoxList
綁定數據:同RadioButtonList同樣,兩種方式: DataSource數據源綁定和 遍歷建立ListItem 綁定
取值:(遍歷建立ListItem方式)
void Button1_Click(object sender, EventArgs e) { string a = ""; foreach(ListItem li in CheckBoxList1.Items) { if (li.Selected) { a += li.Text; } } Label1.Text = a; }
注意:點擊複選框的時候,lable顯示點擊的內容:在checkbox中寫事件SelectedIndexChange,必定要加入自動
提交屬性AutoPostBback="true";
//改變事件代碼: if (CheckBoxList1.SelectedIndex >= 0) Label1.Text = CheckBoxList1.SelectedItem.Text; else Label1.Text = ""; //checkbox中的代碼 <asp:CheckBoxList AutoPostBack="true" ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"></asp:CheckBoxList>
4.DropDownList 下拉列表
賦值:和複合控件checkboxlist,radiobuttonlist同樣
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack == false) { List<Nation> ulist = new NationData().select(); CheckBoxList1.DataSource = ulist; CheckBoxList1.DataTextField = "NationName"; CheckBoxList1.DataValueField = "NationCode"; CheckBoxList1.DataBind(); ListItem la = new ListItem("==請選擇==","-1"); DropDownList1.Items.Add(la); foreach (Nation uu in ulist) { ListItem li = new ListItem(uu.NationName, uu.NationCode); DropDownList1.Items.Add(li); } } Button1.Click += Button1_Click; }
取值
void Button1_Click(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Text; }
5.ListBox 列表控件
屬性:selectionmode設置是否多選,multiple多選,single單選
取值,賦值和控件checkboxlist,radiobuttonlist,dropdownlist同樣
(二)ispostback
綁定數據出現數據選項沒法更改
page_load事件再每一次頁面刷新的時候都會執行
就會把數據從新綁定一次,再去執行按鈕事件
判斷頁面是不是第一次加載仍是響應回發
if(!ispostback)
{
只須要在頁面第一次加載的時候才執行的代碼寫到這裏面
注意95%的代碼都要寫到這裏面
!事件委託不能寫到這裏面
}
(三)跨頁面傳值
1.頁面跳轉:在本窗口中
Response.Redirect("文件路徑");
2.頁面傳值:傳遞的值能夠是不少個,不固定的
用的是QueryString --- url傳值,或者地址欄傳值
接在那個網址後面,就給哪一個傳值,
樣式:地址?key=value&key=value,key就至關因而一個變量,名稱,用來存貯的
接收:string value = Request["key"];
例子:
Response.Redirect("aaa.aspa?a="+TextBox1.Text);
aaa頁面中的接收,而且在label中顯示:string aa = Request["a"]; label1.text=aa;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { Response.Redirect("Default2.aspx?a="+TextBox1.Text+"&b="+TextBox2.Text); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string s=Request["a"]; string ss = Request["b"]; Label1.Text = s+ss; } }
3.打開新頁面窗口:
Response.Write("<script>window.open('Default2.aspx','_blank');</script>");