zoukankan      html  css  js  c++  java
  • 为下拉式菜单(DropDownList)添加第一个选项

    很多方法可以为为下拉式菜单(DropDownList)添加第一个选项,下面是Insus.NET小结了几个方法,仅供参考:

    Html code:

    View Code
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">            
            </asp:DropDownList>
        </div>
        </form>
    </body>

    数据源与绑定:

    View Code
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Data_Binding();
            }
        }

        private void Data_Binding()
        {
            this.DropDownList1.DataSource = Site();
            this.DropDownList1.DataTextField = "key";
            this.DropDownList1.DataValueField = "value";
            this.DropDownList1.DataBind();
        }

        private Dictionary<stringstring> Site()
        {
            Dictionary<stringstring> site = new Dictionary<stringstring>();
            site.Add("Insus.NET cnblogs""http://insus.cnblogs.com");
            site.Add("Microsoft""http://www.microsoft.com");
            site.Add("Google""http://www.google.com");
            return site;
        }

    以下所有方法,均以以上html或code作变动。

    第一种,修改Html Code,把DropDownList属性AppendDataBoundItems的值设为true,然后直接添加一个item:<asp:ListItem Text="--选择--" Value=""></asp:ListItem> 在DropDownList内。

    View Code
     <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">    
            <asp:ListItem Text="--选择--" Value=""></asp:ListItem>        
            </asp:DropDownList>

    第二种方法,Html Code无需更改,

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

    修改Data_Binding()方法,如下: 

    View Code
    private void Data_Binding()
        {
            this.DropDownList1.DataSource = Site();
            this.DropDownList1.DataTextField = "key";
            this.DropDownList1.DataValueField = "value";
            this.DropDownList1.DataBind();

            //添加下面代码:
            DropDownList1.Items.Insert(0new ListItem( "--选择--",""));
             
        }

    第三种方法:

    Html改为如下,设置AppendDataBoundItems属性与及实现OnDataBound事件:

    View Code
    <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" OnDataBound="DropDownList1_DataBound" >                  
            </asp:DropDownList>

    cs code:

    View Code
     protected void DropDownList1_DataBound(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            ddl.Items.Insert(0new ListItem("--选择--""0"));
        }
  • 相关阅读:
    [Xcode 实际操作]六、媒体与动画-(17)使用MediaPlayer框架播放视频
    [Xcode 实际操作]六、媒体与动画-(16)实现音乐的背景播放
    [Xcode 实际操作]六、媒体与动画-(15)使用AudioPlayer播放音乐
    [Xcode 实际操作]六、媒体与动画-(14)使用SystemSoundId播放简短声音
    [Xcode 实际操作]六、媒体与动画-(13)使用UIImageView制作帧动画
    [Swift]LeetCode148. 排序链表 | Sort List
    [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List
    [Swift]LeetCode146. LRU缓存机制 | LRU Cache
    【POJ3615】Cow Hurdles 最短路,你若LCA,我仍不拦你。
    How MapReduce Works
  • 原文地址:https://www.cnblogs.com/insus/p/2252372.html
Copyright © 2011-2022 走看看