给DropDownList 控件加上"==请选择=="选项

1.给VS2003下的DropDownList添加"==请选择=="选项的方法

private void Page_Load(object sender, System.EventArgs e)
{
 if(!IsPostBack)
 {
  String sql="select * from ClassInfo";
  SqlConnection con=new SqlConnection("server=.;uid=sa;pwd=sa;database=ManualAssign");
  SqlDataAdapter sda = new SqlDataAdapter(sql,con);
  DataSet ds =new DataSet();
  sda.Fill(ds, "ClassInfo");
  this.DropDownList1.DataSource = ds.Tables["ClassInfo"].DefaultView;
  this.DropDownList1.DataTextField = "ClassName";
  this.DropDownList1.DataValueField = "ClassId";
  this.DropDownList1.DataBind();
  this.DropDownList1.Items.Insert(0,new ListItem("==请选择==","-1"));//在Item中添加在第0项,该Item的text值为==请选择==,实际的value是-1,注意这句一定要写在this.DropDownList1.DataBind();绑定语句后,写在前面无效
  this.DropDownList1.Items.FindByValue("-1").Selected=true;
  this.DataGrid1.Visible=false;
 }
}
不加上If条件的话,无法选择下拉列表框的值,即使选中了也无法执行,因为当点击按钮时页面重新加载Load方法,默认还是选中“==请选择==”这个选项!

1.给VS2005下的DropDownList添加"==请选择=="选项的方法

请添加一个DropDownList 控件,后台写绑定语句,然后在其Items 中添加一个“==请选择==”项目。

最后一个关键点在于AppendDataBoundItems 属性,请将该属性设置为true,这样便能轻松完成功能的构建。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值