ASP.NET------DropDownList的使用方法
  JiGRrvoIf730 2023年11月02日 30 0



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

.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开发常用方法公共类



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

JiGRrvoIf730