• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

DropDownList的绑定与显示问题

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 28次 | 进入软件测试论坛讨论

领测软件测试网  

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


 }
}

是不是很简单


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网