Label
经常会遇到这种情况,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
{
///
/// Add 的摘要说明。
///
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);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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;
}
}
}
是不是很简单 本文作者:佚名 来源:http://www.bjcan.com
CIO之家 www.ciozj.com 微信公众号:imciow
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读