• λ我爱Aspx >> Asp.Net >> ASP.NET 2.0中给DropDownList服务器控件添加项的新方法
  • ASP.NET 2.0中给DropDownList服务器控件添加项的新方法

  • :aspxer  Դ:网络  :2007-4-25 20:47:17  ؼ:.net,asp.net,asp
  • ASP.NET 2.0中给DropDownList服务器控件添加项的新方法 作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2006年9月12日 23点8分53秒


    在ASP.NET 2.0中,可以在数据绑定时,通过设置DropDownList的AppendDataBoundItems属性为true,在数据绑定之前添加一个新的项目,并且这个新加的项目会保存在ViewState之中。下面就是一个实现的例子:

    C#代码

    <%@ page language="c#" %> <%@ import namespace="system.data" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> ICollection CreateDataSource() &#123; DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("id", typeof(Int32))); dt.Columns.Add(new DataColumn("text", typeof(string))); for (int i = 0; i < 6; i++) &#123; dr = dt.newrow(); dr[0] = i; dr[1] = "列表项目 " + i.tostring(); dt.rows.add(dr); &#125; dataview dv = new dataview(dt); return dv; &#125; protected void button1_click(object sender, eventargs e) &#123; response.write("<li>DropDownList1 您选择的项目:" + DropDownList1.SelectedValue + " ; " + DropDownList1.SelectedItem.Text); Response.Write("<li>DropDownList2 您选择的项目:" + DropDownList2.SelectedValue + " ; " + DropDownList2.SelectedItem.Text); &#125; protected void Page_Load(object sender, EventArgs e) &#123; if (!IsPostBack) &#123; DropDownList1.AppendDataBoundItems = true; DropDownList1.Items.Add(new ListItem("-- 请选择一个选择项 --", "")); DropDownList2.DataSource = DropDownList1.DataSource = CreateDataSource(); DropDownList2.DataTextField = DropDownList1.DataTextField = "text"; DropDownList2.DataValueField = DropDownList1.DataValueField = "id"; DropDownList1.DataBind(); DropDownList2.DataBind(); &#125; &#125; <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>DropDownList 补充例子</title> </head> <body> <form id="form1" runat="server"> </asp:dropdownlist> </asp:listitem> </asp:dropdownlist> </form> </body> </html>

    Ҷƪл˵?
  • һƪ【ASP.NET2.0】使用GridView绑定XmlDocument对象
    һƪASP.NET 2.0中动态添加 GridView 模板列的例子