经常会遇到这种情况,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;
}
}
}
是不是很简单
