WebForm 控件(一)、連接數據庫


一、控件

 

【簡單控件】

(一)文字顯示

1、Label → 在html中相當於span

  <asp:Label ID="控件名 runat="server" Text="顯示的文本"></asp:Label>
2、Literal → 僅文字 → 一般用來輸出JS代碼

  <asp:Literal ID="Literal1" runat="server"></asp:Literal>

 

 

(二)文字輸入

TextBox → TextMode不同效果不同

<asp:TextBox ID="textbox1" runat="server" Enabled="True"></asp:TextBox>
      

      TextMode :默認是Text

                單行文本輸入框singleLine<input name="txtuid" type="text" id="txtuid" disabled="disabled" />

               密碼輸入password<input name="txtpwd" type="password" id="txtpwd" />)

               多行文本輸入motiline<textarea name="txtmemo" rows="2" cols="20" id="txtmemo"></textarea> 


      
Warp:自動換行

      Enabled:是否啟用 相當於html中的disabled是否可見
      ReadOnly:只讀

      Text:相當於value

 

(三)按鈕

1、Button → 默認是html中的Submit(提交按鈕) 無普通按鈕和刷新按鈕,可以直接用input寫

   <asp:Button ID="Button1" runat="server" Text="注  冊" OnClick="Button1_Click" OnClientClick="confirm('really?')" />

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

      OnClientClick:在客戶端點擊時執行服務器上的代碼,字符串屬性,里面寫JS代碼

              例:confirm:confirm('真的要刪除嗎?')默認確定或取消都會刷新頁面,可以用if語句控制

      text:html中的value

2、ImageButton → 圖片按鈕 html中type=image
             ImageUrl:圖片地址

3、LinkButton → 超鏈接樣式的按鈕,僅僅是擁有超鏈接的樣式,並無鏈接

 

控件的相同屬性:
※邊框三項:1、BorderColor:邊框顏色

         2、BorderWidth:邊框粗細        

         3、BorderStyle:邊框樣式

                 NotSet:不設置

                 None:無

                 Dotted:實心不連接方塊

                 Dashed:四角

                 Solid:實線

                 Double:雙實線

                 Groove:下凹效果

                 Ridge:上凸效果

                 Inset:效果同groove

                 Outset:效果同ridge       

Height:高   Width:

 

【復合控件】

DropDownList → select option(html)

 

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

 

顯示數據:(寫在load里面)

方法1:DataSource

        DropDownList1.DataSource = new NationData().Select();//數據源指向
        DropDownList1.DataTextField = "NationName";//顯示字段綁定
        DropDownList1.DataValueField = "NationCode";//隱藏字段綁定
        DropDownList1.DataBind();

方法2:Foreach

        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);
            }
        }    

 

取數據:

1、讀取一條數據
取出value值 或 text值 DropDownList只能取一條

    void Button1_Click(object sender, EventArgs e)
    {
        string end = "";

        foreach (ListItem li in RadioButtonList1.Items)
        {
            if (li.Selected)
            {
                end += li.Text + " - " + li.Value + ",";
            }
        }

        Label1.Text = end;
    }

 

ListBox → select option(html)

 

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>

用法同DropDownList

 

但是可以多選 - SelectionMode

 

CheckBoxList

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

用法同DropDownList

 

RepeatColumns:一行最多顯示多少個數據

RepeatDirection:Vetical垂直顯示  Horizontal水平顯示

RepeatLayout:Table → 用table布局

         Flow → 用span布局

         UnorderedList → 無序列表

         OrderedList → 有序列表

RadioButtonList

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

用法同DropDownList

RepeatColumns:一行最多顯示多少個數據

RepeatDirection:Vetical垂直顯示  Horizontal水平顯示

RepeatLayout:Table → 用table布局

         Flow → 用span布局

         UnorderedList → 無序列表

         OrderedList → 有序列表


 

http協議無狀態性:

每一次事件提交,都會將頁面刷新,刷新就必走Load事件,重復綁定的情況

判斷頁面是第一次加載,還是由已經加載出來的頁面中的某個按鈕執行了提交返回回來的

if (!IsPostBack)

load事件中95%的代碼都要寫在這里面

 


 代碼委托添加點擊事件:

例:

Button1.Click += Button1_Click; 

 


控件中的         name用於服務端       id用於客戶端(js css)使用

 


 


二、WebForm的數據庫連接方式

※放在App_Code文件夾下

※web沒有命名空間

數據庫連接同winform:

1.實力類

2.數據連接類和數據訪問類寫一塊

public class UsersData
{
    SqlConnection conn = null;
    SqlCommand cmd = null;

    public UsersData()
    {
        conn = new SqlConnection("server=.;database=Data0617;user=sa;pwd=123");
        cmd = conn.CreateCommand();
    }

    /// <summary>
    /// 用戶驗證
    /// </summary>
    /// <param name="Uname">驗證的用戶名</param>
    /// <param name="Pwd">驗證的密碼</param>
    /// <returns></returns>
    public bool Select(string Uname, string Pwd)
    {
        bool has = false;
        cmd.CommandText = "select *from Users where UserName =@a and PassWord=@b";
        cmd.Parameters.Clear();
        cmd.Parameters.Add("@a", Uname);
        cmd.Parameters.Add("@b", Pwd);

        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            has = true;
        }
        conn.Close();
        return has;
    }
}

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM