新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > Asp教程 > 正文:DropDownList的绑定与显示问题

DropDownList的绑定与显示问题

新客网 XKER.COM 2005-06-30 来源: 收藏本文
 

经常会遇到这种情况,DropDownList控件大部分用在绑定数据库数据方面居多,但是常常要求它显示在页面上的的时候是一个非数据库中数据的内容,比如“请选择”“全部XX”等等,而实现这种功能的方法简单不简单我就不多说了,直接看源代码吧!

这里假设要绑定的数据库及字段已经建好并确定

绑定数据库字段的代码我就不具体写出来了,假设我把它写在一个名称为“Info”的类中的bindList()方法-->Info.cs类

下面是源代码

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.Text.RegularExpressions;

namespace Client.Page.ClientInfo
{
 /// <summary>
 /// Add 的摘要说明。
 /// </summary>
 public class Add : System.Web.UI.Page
 {
  
  protected Classes.ClientInfo myClass=new Client.Classes.ClientInfo();

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

  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!this.IsPostBack)
   {
        this.BindGrid();
    //this.BindCode();
   }

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

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {    
         this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
  public void BindGrid()
  {
   ArrayList arr=new ArrayList();
   arr.Add(0);
   arr.Add(" ");
   arr.Add(" ");
   this.DropDownList1.DataSource=myClass.bindList();
   this.DropDownList1.DataBind();
   this.DropDownList1.Items.Add("请选择地区");
   this.DropDownList1.Items[this.DropDownList1.Items.Count-1].Value=" ";
   this.DropDownList1.SelectedIndex=this.DropDownList1.Items.Count - 1;
  }


 }
}

是不是很简单


收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐