B/S---控件属性

一、Label控件:标签——呈显出来的时候会变成span标签
    Text - 标签上文字
    <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
    BackColor-背景色
    ForeColor-前景色
    Font
        Bold-加粗
        Italic-倾斜
        UnderLine-下划线        OverLine        StrikeOut
        Name - 字体名
        Size - 字体的大小

    BorderColor-边框颜色
    BorderWidth-边框粗细
    BorderStyle - 边框样式

    Height - 高
    Width - 宽
注: .ss
        {
          display:inline-block;
          color:Red;
          background-color:Black;
          height:50px;
          500px;
        }
默认情况下<span ></span>里面的width和height是不起作用的,但有
display:inline-block;这width和height两个属性就起作用了。

    Enabled-是否可用
    Visible-是否可见
    ToolTip-鼠标指上去的提示信息
    CssClass - 样式表的class选择器


二、Literal:也是一个标签,这个标签不会在两端加上span

三、TextBox:文本框:            HiddenFiled:隐藏域。Value
    拥有Label所有的属性

    TextMode——文本框的呈现模式;
                SingleLine--单行文本框;MultiLine-多行文本框;Password-密码框
    ReadOnly  - 只读
    MaxLength - 最大输入的字符数。只有TextMode是SingleLine和Password的时候起作用,在MultiLine的时候不起作用。

    Columns:宽度,以字母个数为单位
    Rows:高度,以行数为单位。只有TextMode是MultiLine的时候才起作用。在单行文本或多行文本下是不起作用的。

四、Button 按钮:  LinkButton 超链接按钮        ImageButton 图片按钮(ImageUrl属性)
    拥有Label标签的所有属性

    OnClientClick:当按钮被点击的时候,要执行的客户端的JS代码。它的触发要在按钮的C#事件代码之前。

★★★★★★★★★★★★★★★JS的调用技巧★★★★★★★★★★★★★★★★★★★
如何给文本框加JS
 法一:在HTML视图找到相关元素,直接嵌入相关的事件和JS代码。
 例如:

<script language="javascript">
        function dofocus(txt)
        {
            txt.value = "";
        }
    </script>


 设计时候:
 用户名:<asp:TextBox ID="TextBox1" onfocus="doFocus(this)" runat="server" ForeColor="#999999">(必填)</asp:TextBox>

                                                         系统不会提示出来,直接写。
 运行起来:
 用户名:<input name="TextBox1" type="text" value="(必填)" id="TextBox1" onfocus="doFocus(this)" style="color:#999999;" />

 法二:在aspx.cs文件的Page_Load方法中,使用Attributes属性加入JS
例如:
C#代码:
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Attributes.Add("onfocus","doFocus(this)");
    }
运行起来:
密码:<input name="TextBox2" type="text" value="(必填)" id="TextBox2" onfocus="doFocus(this)" style="color:#999999;" />

*******************************************************************************************

五、HyperLink:超链接
拥有Label的所有属性:
    Text -
    NavigateUrl - 超链接的导航地址。相当于href
    Target - 打开位置
    ImageUrl - 图片超链接的图片地址。

六、Image:图像
拥有Label的所有属性:
    ImageUrl - 图片超链接的图片地址。

复合控件
一、下拉列表:DropDownList
拥有Label的所有的属性:
会做三件事情:
(一)把内容填进去
法一:逐项添加
    private void FillNation1()
    {
        //取出数据来
        List<NationData> list = new NationDA().Select();

        //想法扔进去
        foreach (NationData data in list)
        {
            ListItem li = new ListItem(data.Name, data.Code);
            DropDownList1.Items.Add(li);
        }
    }

 //取数据
        List<NationData> list = new NationDA().Select();
        //填上去
        foreach (NationData data in list)
        {
            ListItem li = new ListItem();
            li.Text = data.Name;
            li.Value = data.Code;
            DropDownList1.Items.Add(li);
        }


法二:数据绑定
    private void FillNation2()
    {
        //取出数据来
        List<NationData> list = new NationDA().Select();

         //想法扔进去
        DropDownList1.DataSource = list;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Code";
        DropDownList1.DataBind();  //最终执行绑定填充,不要漏掉
    }

技巧:如何给下拉列表加上“请选择”的功能
1.事先在数据源上加下“请选择”的数据项,在绑定的时候自然会绑定上。

写代码:   ...
        NationData da=new NationData();
        da.Code="-1";
        da.Name="==请选择==";
        list.Insert(0,da);

             ...
2.事先在下拉列表中设置静态的"请选择"列表项。然后再绑定或添加数据的时候在后面添加上。
    如果采用数据绑定模式,默认情况下会把原有的项冲掉。需要设置AppendDataBoundItems属性。

在属性上手动填写 “请选择” 项。


3.所有的项都绑定或填加到下拉列表后,再写代码加上”请选择“的功能。

protected void Page_Load(object sender, EventArgs e)
    {

        FIllnation();
        ListItem li=new ListItem("==请选择==","-1");
        DropDownList1.Items.Insert(0,li);

    }


(二)把选中的值取出来
每次点击按钮时候,都是先执行PageLoad代码,再执行Button的Click代码。  原因?????

if(!IsPostBack)
{
    防止每点提交页面,都会执行这里面的代码。
    这里面的代码,只有页面初次加载的时候才被执行。点击按钮提交的时候,不会被执行到。
    以后记着:在Page_Load事件中99%的情况下需要写这段判断
}

SelectedItem
SelectedValue
SelectedIndex

        //Label1.Text = DropDownList1.SelectedItem.Text + DropDownList1.SelectedItem.Value;
        //Label1.Text = DropDownList1.SelectedValue;
        int index = DropDownList1.SelectedIndex;
        Label1.Text = DropDownList1.Items[index].Text +  DropDownList1.Items[index].Value;

(三)设定某项为选中项
给DropDownList的两个属性赋值:
SelectedIndex = 要选中的索引号
SelectedValue = 要选中项的值


 属性:
 Items -  ListItem的集合
    Add()
    Clear()
    Insert()
    Count
    Remove()
    RemoveAt()

 DataSource
 DataTextField
 DataValueField

 AppendDataBoundItem

 SelectedIndex
 SelectedItem
 SelectedValue

二、RadioButtonList
    拥有DropDownList所有的属性和功能。
    它呈现出来的是单选按钮列表。
    属性:
    RepeatDirection:布局的方向
    RepeatLayout:用表格布局还是流式布局(table/Flow)
    RepeatColumns:一行显示几个

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FIllnation();
            ListItem li = new ListItem("==请选择==", "-1");
            RadioButtonList1.Items.Insert(0, li);
            RadioButtonList1.SelectedIndex = 0;
        }
    }
    private void FIllnation()
    {
        List<NationData> list = new NationDA().Select();
        RadioButtonList1.DataSource = list;
        RadioButtonList1.DataTextField = "Name";
        RadioButtonList1.DataValueField = "Code";
        RadioButtonList1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //取值
        string lbl = RadioButtonList1.SelectedItem.Text + RadioButtonList1.SelectedValue;
        Label3.Text = lbl;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        RadioButtonList1.SelectedIndex = -1;
    }

 三、CheckBoxList
    拥有RadioButton所有的属性和功能。
    呈现出来的是复选框。

    技巧:
    1.如何获取选中的多个项?
        //获取复选框的选中值。
        //思路:遍历复选框列表中的每个项,判断每个项的选中情况。
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                Label2.Text += li.Value + ",";
            }
        }
    2.如何设置某几个项同时选中?
        //设置文本框中指定的项(用 | 隔开的每一项)被选中
        //思路:从文本框中解析出要选中的项的value值,然后再遍历每一项,判断是否是文本框中指定的,是的话就设为选中,不是就设为不选中。
        CheckBoxList1.SelectedIndex = -1;

        string s = TextBox1.Text;
        string[] ss = s.Split('|'); //解析出要选中的value值

        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (ss.Contains(li.Value))
            {
                li.Selected = true;
                continue;
            }
        }

private void Fillnation()
    {
        List<NationData> list = new NationDA().Select();
        CheckBoxList1.DataSource = list;
        CheckBoxList1.DataTextField = "Name";
        CheckBoxList1.DataValueField = "Code";
        CheckBoxList1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Fillnation();
            ListItem li = new ListItem("--请选择--", "-1");
            CheckBoxList1.Items.Insert(0, li);
            CheckBoxList1.SelectedIndex = 0;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        //获取复选框中的值
        //思路:遍历复选框列表中的每一项,判断每一项的选中情况
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected==true)
            {
                Label1.Text += li.Value + "";
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //设置文本框中指定的项(用|隔开的每一项)被选中
        //思路:从文本框中解析出要选中的项的VALUE值,然后再遍历每一项,设置判断是否是文本框中的指定的,是的话就设为选中,不是的话设为不选中
        CheckBoxList1.SelectedIndex = -1;//上来先清空一把
        string s = TextBox1.Text;
        string[] ss=s.Split('|');//解析出要选中的value值

        foreach (ListItem li in CheckBoxList1.Items)
        {
            foreach (string t in ss)
            {
                if (li.Value==t)
                {
                    li.Selected = true;
                    break;
                }       
            }
        } 
    }

四、ListBox:列表框
    拥有DropDownList控件的所有属性。
    SelectionMode - Single,Multiple
    如果是单选的话,照着下拉列表来做。


    如果是多选的话,照着CheckBoxList来做。

例子:下面例子虽然简单,但一定注意:AutoPostBack 属性要设为true,当选定内容后自动提交服务器,这样才能实现三级联动。

 private void Fillchina()
    {
        List<ChinaStateData> list = new ChinaStateDA().SelectParentAreaCode("0001");
        DropDownList1.DataSource = list;
        DropDownList1.DataTextField = "AreaName";
        DropDownList1.DataValueField = "AreaCode";
        DropDownList1.DataBind();
    }
    private void Fillcity()
    {
        string code = DropDownList1.SelectedValue;
        List<ChinaStateData> list = new ChinaStateDA().SelectParentAreaCode(code);
        DropDownList2.DataSource = list;
        DropDownList2.DataTextField = "AreaName";
        DropDownList2.DataValueField = "AreaCode";
        DropDownList2.DataBind();
    }
    private void Fillcounty()
    {
        string code = DropDownList2.SelectedValue;
        List<ChinaStateData> list = new ChinaStateDA().SelectParentAreaCode(code);
        DropDownList3.DataSource = list;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Fillchina();
            Fillcity();
            Fillcounty();
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fillcounty();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fillcity();
        Fillcounty();
    }
原文地址:https://www.cnblogs.com/likaixuan/p/4535269.html