zoukankan      html  css  js  c++  java
  • ASP.NET复合控件

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" AutoPostBack="true"></asp:DropDownList>
                <%--下拉框--%> <%-- AppendDataBoundItems="True"  将数据绑定项追加到静态声明的列表项上,就是加上“===请选择===”这个东西需要改属性--%>
               <%-- AutoPostBack意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为flase就不会刷新了(也就是false时不和服务器交互)--%>
    
                <asp:ListBox ID="ListBox1" runat="server">       </asp:ListBox>
                   
                    
                     <select multiple="multiple">
                        <option>111</option>
                        <option>222</option>
                        <option>333</option>
                        <option>444</option>
                        <option>555</option>
                        <option>666</option>
                        <option>777</option>
                         </select>
         
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </form>
    </body>

    复制代码
      protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
            DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;  // 属性里面设置了AutoPostBack="true",当选择选项时候就触发事件,
    然后label1就等于你选择的那个选项的值,下面在事件里写赋值; if(!IsPostBack) { ListItem li = new ListItem("===请选择===",""); //""里面随便写,如果不选择 就直接请选择 点击按钮会出现你“”里面写的东西 DropDownList1.Items.Add(li); //加一个请选择要想让他在页面显示,要把DropDownList1的属性里面的AppendDataBoundItems的属性改为True; DropDownList1.DataSource = new UsersData().SelectAll(); DropDownList1.DataTextField = "nickname"; DropDownList1.DataValueField = "ucode"; DropDownList1.DataBind(); ListBox1.Items.Add(li); //加一个请选择要想让他在页面显示,要把DropDownList1的属性里面的AppendDataBoundItems的属性改为True; ListBox1.DataSource = new UsersData().SelectAll(); ListBox1.DataTextField = "nickname"; ListBox1.DataValueField = "ucode"; ListBox1.DataBind(); } } void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Text; } void Button1_Click(object sender, EventArgs e) { if(DropDownList1.SelectedItem.Value != "-1") { Label1.Text = DropDownList1.SelectedValue; } }
    复制代码
    复制代码
    <body>
        <form id="form1" runat="server">
            <div>
         <%--       <asp:CheckBox ID="CheckBox1" runat="server" Text="哈哈" />--%>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">  
               <%--     RepeatColumns="3" 每行显示三列--%>     <%--RepeatDirection="Horizontal"水平布局,还有垂直布局,到时候自己从设计里属性里看--%>
                </asp:CheckBoxList>
                <%--就这一句话 在网页打开后什么都不显示,在设计里面,有一个未选定的,右键,编辑项,添加text和value--%>
    
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </form>
    </body>
    复制代码
    复制代码
      protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
            if (IsPostBack==false)
            {
                //赋值,无默认选中项的;
                CheckBoxList1.DataSource = new UsersData().SelectAll();
                CheckBoxList1.DataTextField = "nickname";  //DataTextField获取或设置为列表项提供文本内容的数据源字段
                CheckBoxList1.DataValueField = "ucode";    //DataValueField获取或设置为列表项提供文本内容的数据源字段   如果不写这两句话 他只会为你提供一个Users 不会给你提供名字,这里就是为了给你提供名字(显示的名字)
                CheckBoxList1.DataBind();
                
                //赋值有默认选中项的 需要遍历
                //List<Users> ulist = new UsersData().SelectAll();
                //foreach (users u in ulist)
                //{
                //    listitem li = new listitem(u.nickname, u.username);
    
    
                //    if (u.username == "xiaoyueyue" || u.username == "liuyubin")
                //        li.selected = true;  //获取或设置一个值,该值指示是否选定此项,表名已经选定这几项
                //    checkboxlist1.items.add(li);
    
                //}
            }
        }
        void Button1_Click(object sender, EventArgs e)
        {
            //if (CheckBox1.Checked)  
            //    Label1.Text = CheckBox1.Text;
            // 取值
            if (CheckBoxList1.SelectedItem != null)   // if (CheckBoxList1.SelectedIndex != -1)    不能为空,要不会报错
            {
                //取一个值,如果选择多个会取第一个,SelectedItem索引最小的那个
                // Label1.Text = CheckBoxList1.SelectedItem.Text;  //获取选中最小的值,还有.value
    
                //取多个值,遍历
                string s = "";
                foreach (ListItem li in CheckBoxList1.Items)   //遍历 每一个都是ListItem对象,
                {
                    if (li.Selected)   //Selected,获取或设置一个值,该值指示是否选定此项
                    {
                        s += li.Value + ",";  //加等于 后面的text也可以换成value就成用户的编号;
                        //s += li.Text + ",";
                    }
                }
                Label1.Text = s;
            }
    
    
        }

     

     
  • 相关阅读:
    171. Excel Sheet Column Number (Easy)
    349. Intersection of Two Arrays (Easy)
    453. Minimum Moves to Equal Array Elements (Easy)
    657. Judge Route Circle (Easy)
    CSS笔记
    保存页面状态
    UI开发总结
    ubuntu 下配置munin
    反向代理配置
    JavaScript 高级程序设计第二版
  • 原文地址:https://www.cnblogs.com/wcl2017/p/7339700.html
Copyright © 2011-2022 走看看