C#_控件——DropDownList

1.html

            <asp:CheckBox ID="CheckBox11" runat="server" onclick="changechecked(this)" />车身颜色:
            <asp:DropDownList ID="DropDownList5" runat="server">
                <asp:ListItem Text="红色" Value="红色的" Enabled="true" Selected="True"></asp:ListItem>
                <asp:ListItem Text="绿色" Value="绿色的" />
            </asp:DropDownList>

2.code

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Text一般用来前台显示给用户看的
                //Value一般用于表单提交,提交给后台的数据
                string l5_text = DropDownList5.SelectedItem.Text;//红色
                string l5_value = DropDownList5.SelectedItem.Value;//红色的
                string l5_value1 = DropDownList5.SelectedValue.ToString();//红色的      
                string text1 = DropDownList5.Items[1].Text;//绿色
                string value1 = DropDownList5.Items[1].Value;//绿色的
                bool IsSelected = DropDownList5.Items[1].Selected; //绿色的
                //t添加动态数据给下拉列表
                DataSet ds = new DataSet();//此处只是举例,DataSet一定要实例化的
                DropDownList5.DataSource = ds.Tables["dmsm1"].DefaultView;//"dmsm1"为数据库的名称
                DropDownList5.DataTextField = "dmz";//dmz为数据库的字段名称
                DropDownList5.DataValueField = "dmz";  
                DropDownList5.DataBind();
                //添加静态数据给下拉列表
                DropDownList5.Items.Insert(0,"----请选择----");
                DropDownList5.Items[1].Selected = false;
                DropDownList5.Items[0].Selected = true ;
                //DropDownList5.Items[1].Selected = true;
            }
            bool check2 = CheckBox2.Checked;
        }

 3.

 

原文地址:https://www.cnblogs.com/slu182/p/4259601.html