zoukankan      html  css  js  c++  java
  • AppendDataBoundItems实现追加绑定

    当对一个列表控件执行DataBind()会清空之前的所有数据项,因为很多时候如果加一个特殊项的话会选择在绑定完成后动态插入一条,就像下面这样

    //下面前台代码 
     <asp:DropDownList ID="ddlCity" runat="server">
    <asp:ListItem Text="全部" Value="0"  Selected="True"></asp:ListItem> </asp:DropDownList> //下面后台代码 Dictionary<string, string> citys = new Dictionary<string, string>(); citys.Add("北京", "1"); citys.Add("上海", "2"); ddlCity.DataSource = citys; ddlCity.DataTextField = "Key"; ddlCity.DataValueField = "Value"; ddlCity.DataBind(); ddlCity.Items.Insert(0, new ListItem("全部", "0"));

    现在有了AppendDataBoundItems属性,表示是否追加绑定项,默认为false表示不追加,我们改为true就不需要像上面那样的写法了

    //下面前台代码 
     <asp:DropDownList ID="ddlCity" runat="server">
     <asp:ListItem Text="全部" Value="0"  Selected="True"></asp:ListItem>
     </asp:DropDownList>
    //下面后台代码
     Dictionary<string, string> citys = new Dictionary<string, string>();
     citys.Add("北京", "1");
     citys.Add("上海", "2");
     ddlCity.AppendDataBoundItems = true;//加入这个设置
     ddlCity.DataSource = citys;
     ddlCity.DataTextField = "Key";           
     ddlCity.DataValueField = "Value";
     ddlCity.DataBind();

    而且如果多次调用DataBind()方法,也只会在后面追加数据而已,不管前面绑定过什么数据了,哈哈,不错吧

  • 相关阅读:
    39.前端需要注意哪些SEO
    38.http的几种请求方法和区别
    37.伪元素和伪类的区别
    35.如何实现页面每次打开时清除本页缓存
    Jmeter工具设置中文版(切换显示语言)
    soapui测试http接口
    【C++】《Effective C++》第三章
    【C++】《Effective C++》第二章
    【设计模式】面向对象设计原则
    【设计模式】设计模式概述
  • 原文地址:https://www.cnblogs.com/fuyun2000/p/3017380.html
Copyright © 2011-2022 走看看