WebForm 常用控制項

來源:互聯網
上載者:User

標籤:.sql   conf   textarea   語句   pre   object   onclick   start   display   

一、簡單控制項

1、Label(作用:顯示文字)

Web中:

<asp:Label ID="Label1" runat="server" Text="Label" BorderColor="Black" BorderStyle="Solid" BorderWidth="5px"></asp:Label>

編譯完成後的元素時span(html)

<span id="Label1" style="display:inline-block;border-color:Black;border-width:5px;border-style:Solid;">Label</span>

屬性:①BackColor:控制項背景色 ;

        ②BorderColor:控制項邊框顏色;

        ③BorderStyle:控制項邊框樣式;

        ④BorderWidth:控制項邊框寬度

 

2、Literal(作用:顯示文字)
Web中:

<asp:Literal ID="Literal1" runat="server" Text ="編譯後不會形成什麼元素"></asp:Literal>

編譯後不會形成什麼元素(一般用來後台輸出js代碼)

</div>        編譯後不會形成什麼元素

3、TextBox(文字輸入框)

屬性:①TextMode:文本礦的行為模式,有以下幾種模式:

★預設SingleLine:單行。

Web中:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

編譯後:

<input name="TextBox1" type="text" id="TextBox1" />

★Password:密碼框

Web中:

<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>

編譯後:

<input name="TextBox1" type="password" id="TextBox1" />

★Multiline:文本域

Web中:

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>

編譯後textarea:

<textarea name="TextBox1" rows="2" cols="20" id="TextBox1">

        ②warp:換行(true:換行;false:不換行)

        ③Enabled:控制項啟用狀態

        ④Readonly:是否可以更改控制項中的文本

        ⑤Maxlength:限制最長長度;比如:密碼限制多少位,就可以更改此屬性

4、按鈕類

(1)Button:

Web中:

<asp:Button ID="Button1" runat="server" Text="Button" />

編譯後submit:

<input type="submit" name="Button1" value="Button" id="Button1" />

屬性:Onclintclick:比如:在onclintclick後面加上alert("nihao");
編譯後是:

<input type="submit" name="Button1" value="Button" onclick="alert(&quot;nihao&quot;);" id="Button1" />

註:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick=‘if(confirm("是否要提交?")){return false;}‘ />

Confirm():

       confirm() 方法用於顯示一個帶有指定訊息和OK 及取消按鈕的對話方塊。

  如果使用者點擊確定按鈕,則confirm() 返回true。如果點擊取消按鈕,則confirm() 返回false。

  在使用者點擊確定按鈕或取消按鈕把對話方塊關閉之前,它將阻止使用者對瀏覽器的所有輸入。在調用confirm() 時,將暫停對JavaScript 代碼的執行,在使用者作出響應之前,不會執行下一條語句。

  下面我們通過這兩個小例子,來瞭解一下它的使用方法吧:

<head><title>confrim 的使用方法</title><script type="text/javascript">function clear1(){ if(confirm("確定要清空資料嗎?")) { document.main.text1.value=""; }}</script></head><boty><form name="main"><input type="text" name="text1"/><input type="button" name="submit" value="資料清空" onclick="return clear1()"/></form></body>
confrim 的使用方法

(2)ImageButton:圖片按鈕
        屬性同Button類似,多以個ImageUrl屬性,此屬性用於存放圖片地址。

(3)LinkButton:被編輯成超連結模樣的按鈕,

 

注:①HyperLink:超連結控制項(不經常用此方式見超連結)

      ②邊框注意:邊框顏色——邊框樣式——邊框粗細

二、複合控制項

      首先建兩個類,下面的複合控制項將會用到!

實體類:

/// <summary>/// Nation 的摘要說明/// </summary>public class Nation{    public Nation()    {        //        // TODO: 在此處添加建構函式邏輯        //    }    private string _NationCode;    /// <summary>    /// 民族編號    /// </summary>    public string NationCode    {        get { return _NationCode; }        set { _NationCode = value; }    }    private string _NationName;    /// <summary>    /// 民族名稱    /// </summary>    public string NationName    {        get { return _NationName; }        set { _NationName = value; }    }}
View Code

 

資料訪問類:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.SqlClient;/// <summary>/// NationData 的摘要說明/// </summary>public class NationDA{    SqlConnection conn = null;    SqlCommand cmd = null;    public NationData()    {        conn = new SqlConnection("server=.;database=Data0617;user=sa;pwd=123");        cmd = conn.CreateCommand();    }    /// <summary>    /// 返回全部Nation表資料集合    /// </summary>    /// <returns></returns>    public List<Nation> Select()    {        List<Nation> list = new List<Nation>();        cmd.CommandText = "select *from Nation";        conn.Open();        SqlDataReader dr = cmd.ExecuteReader();        if (dr.HasRows)        {            while (dr.Read())            {                Nation n = new Nation();                n.NationCode = dr["NationCode"].ToString();                n.NationName = dr["NationName"].ToString();                list.Add(n);            }        }        conn.Close();        return list;    }
View Code

 

(一)DropDownList:下拉式清單方塊

Web顯示:

<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

編譯後select:

<select name="DropDownList1" id="DropDownList1"></select>

1、給DropDownList寫入資料(兩種方法)——放在Page_Load中
法一:與winform中給下拉表框填資料類似(DataSource)

 protected void Page_Load(object sender, EventArgs e)    {        DropDownList1.DataSource = new NationData().Select();//資料來源指向        DropDownList1.DataTextField = "NationName";//顯示欄位綁定        DropDownList1.DataValueField = "NationCode";//隱藏欄位綁定        DropDownList1.DataBind();      }
View Code

法二:Foreach遍曆,同時加上預設選中

 protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            List<Nation> Nlist = new NationData().Select();            foreach (Nation n in Nlist)            {                ListItem li = new ListItem(n.NationName, n.NationCode);                if (li.Value == "N003")                {                    li.Selected = true;                }                DropDownList1 . Items.Add(li);            }        }    }
View Code

編譯後顯示:

<select name="DropDownList1" id="DropDownList1">    <option value="N001">漢族</option>    <option value="N002">滿族</option>    <option selected="selected" value="N003">藏族</option>    <option value="N004">彝族</option></select>
View Code

注:加一個Button和Label,點擊按鈕時,將取到的value或text顯示在label上。下面用到

 

2、取DropDownList的Value或者text(只能取一條資料的value或text)

void Button1_Click(object sender, EventArgs e)    {        Label1.Text = DropDownList1.SelectedItem.Value;        //Label1.Text = DropDownList1.SelectedItem.Text;    }

3、取多條資料(ListBox控制項)

ListBox控制項(此控制項可以取一條或多條資料)——編譯後也是select(下拉式清單方塊)
屬性:SelectionMode(列的選擇模式)——single:單行,只單選;Multiple:多行,可多選。

ListBox綁定資料的方法同DropDownList一樣。

ListBox取資料的方法:

void Button1_Click(object sender, EventArgs e)    {        string end = "";        foreach (ListItem li in ListBox1.Items)        {            if (li.Selected)            {                end += li.Text + " - " + li.Value + ",";            }        }        Label1.Text = end;    }
View Code

(二)CheckBoxList:多選列表

<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatLayout="UnorderedList"></asp:CheckBoxList>

屬性:①RepeatColumns:一行最多顯示多少個資料

        ②RepeatDirection——Vetical:垂直顯示 ; Horizontal:水平顯示

        ④RepeatLayout:Table → 用table布局

                   Flow → 用span布局

                   UnorderedList → 無序列表

                   OrderedList → 有序列表

用法同DropDownList和ListBox!

(三)RadioButtonList

<asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>

屬性同CheckBoxList類似,用法同DropDownList和ListBox!

★控制項中,name用於服務端 , id用於用戶端

WebForm 常用控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.