ASP.NET------DropDownList的使用方法


第一种少量自定义数据时:

.aspx中的代码:

<asp:DropDownList ID="DropDownList1" runat="server">
                   <asp:ListItem Value="2">男</asp:ListItem>
                   <asp:ListItem Selected="True" Value="1">女</asp:ListItem>
               </asp:DropDownList>

.cs中的代码: 注意后台取值有取文字和编号两种


 this.DropDownList1.SelectedItem.Text   //取文字 既男女
  this.DropDownList1.SelectedValue.ToString()  //取编号 



第二种从数据库中取数据进行绑定:
  DataSet dc= new DataSet();  

            sqlStr = "select book_class_id,book_class_name from book_class";


            dc = CC.GetDataSet(sqlStr, "00");   //取出数据放入表中,这里的CC是我自己的一个公共类 ,反正这里是取出数据放到一个表中就行了,后面给出常用公共类的代码
            this.DropDownList1.DataSource = dc.Tables[0];
            this.DropDownList1.DataTextField = "book_class_name"; //绑定文字对应的字段
            this.DropDownList1.DataValueField = "book_class_id";       //绑定编号对应的字段
            this.DropDownList1.DataBind();

后台.cs中取数据的方法同第一种方法



特殊:给DropDownList设默认值

  

   this.DropDownList1.SelectedValue = 3;   // 3是编号
       this.DropDownList1.Items.FindByText(“计算机”).Selected = true;  //计算机是文字
 this.DropDownList1.Items.FindByValue("3").Selected = true; // 3是编号 


添加默认选项

  this.DropDownList1.Items.Insert(0, new ListItem("---选择类型---", "0"));
            this.DropDownList1.SelectedIndex = 0;





点击查看Web开发常用方法公共类



  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张小凡vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值