標籤:body 接受 hidden bsp iter false select ons return
一.記憶Web端控制項需要配合HTML 中的Form表單元素
1、Label
會被編譯成span標籤
屬性:
Text:常值內容
CssClass:CSS樣式
Enlabled:是否可用
Visible:是否可見
2、Literal
空的,C#會把裡面的Text內容直接作為網頁代碼傳過去,比如Text裡面寫上<input type="button" />會直接在網頁中插入一個按鈕
屬性:
Text:內容
文本類
- 文字框 <input type="text"> TextBox
- 密碼框 <input type="password"> TextBox 屬性TextMode="password"
- 文本域 <textarea> TextBox 屬性TextMode="MutilLine"
- 隱藏文本域 <input type="hidden> HiddenFiled
取值用value
按鈕類
- 普通按鈕 button 無
- 提交按鈕 submit Button(預設提交)
- 重設按鈕 reset 無
- 圖片按鈕 image ImageButton
選擇類
1、RadioButton 和 RadioButtonList
選項按鈕
大多情況下使用後者
綁定資料:
RadioButtonList1.DataSource = 泛型集合;
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "Code";
RadioButtonList1.DataBind(); - 必須要有
設定選中項:
按照索引選中:
RadioButtonList1.SelectedIndex = slist.Count - 1;
按照value值選中:
RadioButtonList1.SelectedValue = "002";
按照Text選中:
foreach (ListItem li in RadioButtonList1.Items)
{
if (li.Text == "漢族")
{
li.Selected = true;
}
}
取出資料:
取出value值
Label1.Text = RadioButtonList1.SelectedValue;
取出Text值
Label1.Text = RadioButtonList1.SelectedItem.Text;
2、CheckBox 和 CheckBoxList
複選按鈕
綁定資料來源與設定單個選擇項同上,如果要設定多個選擇項,則需要遍曆
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected == true)
{
Label1.Text += li.Text + ",";
}
}
三.關於綁定資料重新整理似的問題
綁定資料出現資料選項無法更改
page_load事件再每一次頁面重新整理的時候都會執行
就會把資料重新綁定一次,再去執行按鈕事件
判斷頁面是否是第一次載入還是響應回傳
if(!ispostback)
{
只需要在頁面第一次載入的時候才執行的代碼寫到這裡面
注意95%的代碼都要寫到這裡面
!事件委託不能寫到這裡面
}
四.頁面傳值
傳值取值 - 跨頁面傳值
QueryString - url傳值,地址傳值
傳值:[地址?key=value&key=value]
取出url傳遞過來的值
取值:string value = Request["key"];
原理:地址欄後面接 ?key=value&key=value 在後台代碼中可以通過這些key來接受需要的字串
優點:不佔用伺服器記憶體,執行快 缺點:直接顯示在了地址欄上,安全性差
五.Response: - 響應請求對象
Response.Redirect("想要跳轉的頁面地址");- 服務端重新導向
Response.Write("想要在頁面中插入的語句,例如:<script>alert(‘123321‘);</script>");- 將此字串輸出到頁面的最頂部
補充
<asp:Button OnClientClick="return true(點擊可以提交) false(點擊不會提交重新整理)" ID="Button1" runat="server" Text="添加" />
05-19Web端控制項,頁面傳值