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

 

 

 

 

 

原文地址:https://www.cnblogs.com/ShenG1/p/5888979.html