也谈谈动态绑定dropdownlist(2)

来源:互联网 发布:python excel 修改 编辑:程序博客网 时间:2024/06/08 07:35
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

也谈谈动态绑定dropdownlist2

 

       在我的《也谈谈动态绑定dropdownlist1》的文章里,(http://blog.csdn.net/zsxfbj/archive/2004/07/08/36659.aspx)提到了的是利用dataset作为数据源来实现datasetItem绑定。但是DataSet包含的内容和结构太多,我们只要求的是快速的绑定DropDownListItem,而不对数据做任何的操作。所以说用DataSet做数据源的话,是不是有些大材小用的感觉?

       而且在用DataSet做为数据源的时候,我们要指定:

DropDownList1.DataTextField = "ItemName"; //dropdownlistText的字段

DropDownList1.DataValueField = "id";//dropdownlistValue的字段

这样的话,我们还要知道表的字段,这个方面不是很好。如果我们想在绑定一个叫Text为:All Item,Value为0的Item,用DataSet作为数据源时绑定会出现问题,我在绑定DropDownList1时,先指定上面我要加的Item项:

     DropDownList1.Items.Add( new ListItem( "ALL Item", "0" ) );//这里为新加代码

    DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;

    //指定DropDownList使用的表里的那些字段

    DropDownList1.DataTextField = "ItemName"; //dropdownlistText的字段

    DropDownList1.DataValueField = "id";//dropdownlistValue的字段

    DropDownList1.DataBind();

编译后生成的页面的代码:

<select name=”DropDownList1” id=”DropDownList1”>

<option value=”5”>Item5</option>

<option value=”4”>Item4</option>

<option value=”3”>Item3</option>

<option value=”2”>Item2</option>

<option value=”1”>Item1</option>

</select>

新加的All Item这项根本没有。如果发在后面呢?

     //指定DropDownList使用的数据源

    //DropDownList1.Items.Add( new ListItem( "ALL Item", "0" ) );//新加的代码

    DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;

    //指定DropDownList使用的表里的那些字段

    DropDownList1.DataTextField = "ItemName"; //dropdownlistText的字段

    DropDownList1.DataValueField = "id";//dropdownlistValue的字段

    DropDownList1.DataBind();

    DropDownList1.Items.Add( new ListItem( "ALL Item", "0" ) );//新加的代码

编译后的页面的代码为:

<select name="DropDownList1" id="DropDownList1">

     <option value="5">Item5</option>

     <option value="4">Item4</option>

     <option value="3">Item3</option>

     <option value="2">Item2</option>

     <option value="1">Item1</option>

     <option value="0">ALL Item</option>

</select>

好像<option value="0">ALL Item</option>这项有了,但是是放在了最下面,这又不符合我们的一般的习惯。那么怎么办呢?

     既然,DropDownList1.Items可以Add一个new ListItem,而且DataSet做数据源太浪费,我们又不对数据做任何修改,那么我们只是Read一下就可以了。下面就看看这段代码:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Configuration;

 

namespace BindDropDownList

{

    /// <summary>

    /// Example2 的摘要说明。

    /// </summary>

    public class Example2 : System.Web.UI.Page

    {

      protected System.Web.UI.WebControls.DropDownList DropDownList1;

      protected System.Web.UI.WebControls.Button Button1;

  

        private void Page_Load(object sender, System.EventArgs e)

        {

            // 在此处放置用户代码以初始化页面

        }

 

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)

        {

            //

            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。

            //

            InitializeComponent();

            base.OnInit(e);

        }

       

        /// <summary>

        /// 设计器支持所需的方法 - 不要使用代码编辑器修改

        /// 此方法的内容。

        /// </summary>

        private void InitializeComponent()

        {   

         this.Button1.Click += new System.EventHandler(this.Button1_Click);

         this.Load += new System.EventHandler(this.Page_Load);

 

      }

        #endregion

 

      private void Button1_Click(object sender, System.EventArgs e)

      {

         //取得Web.config里的数据库连接字串

         string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];

         //创建一个SqlConnection

         SqlConnection Conn = new SqlConnection( ConnString );

        

         string SQL_Select = "select id, ItemName from DDLItem order by id desc";

         //创建一个SqlCommand

         SqlCommand myCommand = new SqlCommand( SQL_Select, Conn );

         //读取数据记录并绑定

         myCommand.Connection.Open();

         //使用DataReader读取速度更快

         SqlDataReader myReader = myCommand.ExecuteReader();

         while ( myReader.Read() )

         {

            DropDownList1.Items.Add( new ListItem( myReader["ItemName"].ToString(),myReader["id"].ToString() ) );//增加Item

            //或者这样也能绑定,

            //DropDownList1.Items.Add( new ListItem( myReader[1].ToString(),myReader[0].ToString() ) );//增加Item

            //都是要在知道Sql语句或者数据表结构的前提下才能这样绑定

         }

 

         myCommand.Connection.Close();

 

      }

    }

}

编译运行后,效果一样,但是更节省了系统的开销。而且我们也可以方面的添加特别的Item,比如这样:

    private void Button1_Click(object sender, System.EventArgs e)

      {

         DropDownList1.Items.Add( new ListItem( "ALL Item", "0" ) );//新加一个Item

         //取得Web.config里的数据库连接字串

         string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];

         //创建一个SqlConnection

         SqlConnection Conn = new SqlConnection( ConnString );

        

         string SQL_Select = "select id, ItemName from DDLItem order by id desc";

         //创建一个SqlCommand

         SqlCommand myCommand = new SqlCommand( SQL_Select, Conn );

         //读取数据记录并绑定

         myCommand.Connection.Open();

         <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

原创粉丝点击