DropDownList的绑定与显示问题

王朝网络·asp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

经常会遇到这种情况,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;

}

}

}

是不是很简单

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有