DropDownList的绑定与显示问题
2005-06-27 11:01
225 查看
经常会遇到这种情况,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;
}
}
}
是不是很简单
这里假设要绑定的数据库及字段已经建好并确定
绑定数据库字段的代码我就不具体写出来了,假设我把它写在一个名称为“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;
}
}
}
是不是很简单
相关文章推荐
- DropDownList绑定出现控件中显示的是“System.Data.DataRowView”问题记录
- DropDownList绑定显示问题! ——转
- DropDownList的绑定与显示问题
- 绑定到DropDownList,设定Text和value显示
- 如何使用 BindingSource 绑定 ListBox,同时解决绑定 List<T> 后修改数据源不能同时刷新界面显示的问题
- DataGrid Repeater关于绑定列过长截断显示的问题
- SenchaTouch2中list组件无法绑定存储或者绑定后仍旧无法显示数据问题解决
- DataGridView绑定数据时,时间格式显示不全的问题
- DataGridView绑定数据时,时间格式显示不全问题
- 关于Dropdownlist绑定GridView分页出错问题
- DropDownList绑定的问题
- Asp.net中在GridView数据绑定事件中改变显示内容要注意的问题
- DataGridComboBoxColumn绑定后显示空白的问题
- 超实用,解决img标签src绑定了正确地址后,图片有时候不显示问题
- Datagrid数据绑定后,根据不同条件判断显示问题
- SenchaTouch2中list组件无法绑定存储或者绑定后仍旧无法显示数据问题解决
- 三维软件开发笔记---调错DataGrid控件数据绑定无法显示问题
- DropDownList绑定选择数据报错问题
- .Net 绑定Dropdownlist的时自定义组合字段后显示
- C# ComboBox 绑定 显示 System.Data.DataRowView 问题解决