教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> .NET类 >> ASP.NET >> ASP.NET 2.0中给DropDownList服务器控件添加项的新方法 RSS订阅
ASP.NET 2.0中给DropDownList服务器控件添加项的新方法
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
     在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">
  
  <script runat="server">
   ICollection CreateDataSource()
   {
   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++)
   {
   dr = dt.NewRow();
   dr[0] = i;
   dr[1] = "列表项目 " + i.ToString();
   dt.Rows.Add(dr);
   }
   DataView dv = new DataView(dt);
   return dv;
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
   Response.Write("<li>DropDownList1 您选择的项目:" + DropDownList1.SelectedValue
   + " ; " + DropDownList1.SelectedItem.Text);
   Response.Write("<li>DropDownList2 您选择的项目:" + DropDownList2.SelectedValue
   + " ; " + DropDownList2.SelectedItem.Text);
   }
  
   protected void Page_Load(object sender, EventArgs e)
   {
   if (!IsPostBack)
   {
   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();
   }
   }
  </script>
  
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
   <title>DropDownList 补充例子</title>
  </head>
  <body>
  <form id="form1" runat="server">
   <asp:DropDownList ID="DropDownList1" runat="server">
   </asp:DropDownList>
   <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true">
   <asp:ListItem Text="请选择" Value=""></asp:ListItem>
   </asp:DropDownList>
   <asp:Button ID="Button1" runat="server" Text="得到选择的值" OnClick="Button1_Click" />
  </form>
  </body>
  </html>
  VB.NET代码
  
  <%@ Page Language="VB" AutoEventWireup="true" %>
  
  <%@ Import Namespace="System.Data" %>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
  <script runat="server">
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Response.Write("<li>DropDownList1 您选择的项目:" + DropDownList1.SelectedValue + _
   " ; " + DropDownList1.SelectedItem.Text)
   Response.Write("<li>DropDownList2 您选择的项目:" + DropDownList2.SelectedValue + _
   " ; " + DropDownList2.SelectedItem.Text)
   End Sub
  
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
   If Not IsPostBack Then
   DropDownList1.AppendDataBoundItems = True
   DropDownList1.Items.Add(New ListItem("-- 请选择一个选择项 --", ""))
   DropDownList2.DataSource = CreateDataSource()
   DropDownList1.DataSource = CreateDataSource()
   DropDownList2.DataTextField = "text"
   DropDownList1.DataTextField = "text"
   DropDownList2.DataValueField = "id"
   DropDownList1.DataValueField = "id"
   DropDownList1.DataBind()
   DropDownList2.DataBind()
   End If
   End Sub
  
   Function CreateDataSource() As ICollection
   Dim dt As DataTable = New DataTable()
   Dim dr As DataRow
   dt.Columns.Add(New DataColumn("id", GetType(System.Int32)))
   dt.Columns.Add(New DataColumn("text", GetType(String)))
   For i As Integer = 0 To 6
   dr = dt.NewRow()
   dr(0) = i
   dr(1) = "列表项目 " + i.ToString()
   dt.Rows.Add(dr)
   Next
   Dim dv As DataView = New DataView(dt)
   Return dv
   End Function
  
  </script>
  
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head id="Head1" runat="server">
   <title>DropDownList 补充例子</title>
  </head>
  <body>
   <form id="form1" runat="server">
   <asp:DropDownList ID="DropDownList1" runat="server">
   </asp:DropDownList>
   <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true">
   <asp:ListItem Text="请选择" Value=""></asp:ListItem>
   </asp:DropDownList>
   <asp:Button ID="Button1" runat="server" Text="得到选择的值" OnClick="Button1_Click" />
   </form>
  </body>
  </html>
  
    。



来源:upschool.com.cn
作者:
关键字:ASP.NET,新方法
发表日期:2006-11-29

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:ASP.NET 2.0高级数据处理之冲突检测   下一篇:不走寻常路 设计 ASP.NET应用程序的七大绝招


本文的相类似文章
  • PS精通:羽毛的制作的最新方法
  • 黑客揭密ASP.net服务器的入侵
  • 木马隐藏在Win系统中的新方法
  • 黑客种植木马的新方法及防范策略
  • ASP.Net漏洞 黑客可绕过安全设置
  • ASP.NET中如何防范SQL注入式攻击
  • ASP.NET中树形图的实现 3
  • ASP.NET 2.0中随机读取Access数据库记录
  • ASP.NET中树形图的实现 1
  • ASP.NET中树形图的实现 2
  • 网友评论 查看本文全部评论
    笔 名: *
    评 论:
    最多500字。当前字数:0
    联系方式:
    验证码:
    在学习中进步 在进步中成长 教程中国相随您的成长之路
    华腾联合科技股份有限公司版权所有
    广告联系:Rosibo@163.com