C#_控件——DropDownList

时间:2023-03-09 06:24:41
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[].Text;//绿色
string value1 = DropDownList5.Items[].Value;//绿色的
bool IsSelected = DropDownList5.Items[].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(,"----请选择----");
DropDownList5.Items[].Selected = false;
DropDownList5.Items[].Selected = true ;
//DropDownList5.Items[1].Selected = true;
}
bool check2 = CheckBox2.Checked;
}

3.

C#_控件——DropDownList

C#_控件——DropDownList