绑定DataTable

 ex;
}

绑定枚举
方法一:可绑定Text和Value.

 

(EnumArea), i), i.ToString());
DropDownList1.Items.Add(item);
}

方法二:只绑定Text.

 

(EnumArea));
DropDownList1.DataBind();

 

 

dropdownlist动态绑定

...
DataSet ds=new DataSet();
SqlDataAdapter command=new SqlDataAdapter("",conn);
...
conn.Open();    //打开数据库连接
command.SelectCommand.CommandText="Select a,b FROM tb1";
command.Fill(ds,"table1");
conn.Close();

方法一:
不设定dropdownlist的datatextfield,datavaluefield

for(int i=0;i<ds.Tables["table1"].Rows.Count;i++)
{
   dropdownlist.Items.Add(new ListItem(ds.Tables["table1"].Rows[i][1].ToString(),ds.Tables["table1"].Rows[i][0].ToString()));
}

方法二:
设定dropdownlist的datatextfield="b",datavaluefield="a"
 DataSet  ds = m.ManagerRead();
        b_operator.DataSource = ds.Tables[0];
        b_operator.DataTextField = "a_name";
        b_operator.DataValueField = "id";
        b_operator.DataBind();
        ds.Clear();
        ds.Dispose();

从数据库中读出DropDownList的选中值

从数据库中读出DropDownList的选中值
dropdownlist.aspx
<asp:DropDownList ID="DropDownList1" runat="server">
 <asp:ListItem>语文</asp:ListItem>
 <asp:ListItem>体育</asp:ListItem>
 <asp:ListItem>数学</asp:ListItem>
</asp:DropDownList>

dropdownlist.cs
string str = "体育";//这一项可以从数据库中读出
for (int j = 0; j < this.DropDownList1.Items.Count; j++)
{
 if (str == this.DropDownList1.Items[j].Text.ToString())
 {
  this.DropDownList1.Items[j].Selected = true;
 }
}

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2021-08-27
  • 2022-02-23
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2021-06-12
相关资源
相似解决方案