DropDownList
June 21, 2010 Leave a comment
Fill DropDownList
public SqlConnection DBConnect()
{
try
{
con = new SqlConnection(ConnectionStr);
if (con.State != ConnectionState.Open)
{
con.Open();
}
return con;
}
catch (Exception)
{
throw;
}
}
private void FillRequestType()
{
try
{
ds = new DataSet();
ds.Clear();
da = new SqlDataAdapter(Qry, DBConnect());
da.Fill(ds);
con.Close();
Ddl_RequestTypeAuthority.DataSource = ds;
Ddl_RequestTypeAuthority.DataTextField = “RequestType“;
Ddl_RequestTypeAuthority.DataValueField = “DepartmentID“;
Ddl_RequestTypeAuthority.DataBind();
Ddl_RequestTypeAuthority.Items.Insert(0, “Select”);
}
catch (Exception)
{
throw;
}
}
Bind an XML to DropDownList ASP.Net C#
Employees.xml
<Employees >
<Employee >
<Name >David </Name >
<ID >101 </ID >
<IsActive >true </IsActive >
</Employee >
<Employee >
<Name >Tom </Name >
<ID >102 </ID >
<IsActive >true </IsActive >
</Employee >
<Employee >
<Name >Rick </Name >
<ID >103 </ID >
<IsActive >false </IsActive >
</Employee >
<Employee >
<Name >Mark </Name >
<ID >104 </ID >
<IsActive >true </IsActive >
</Employee >
</Employees >
——————————————————–
Code sample:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath(“Employees.xml”));
DataView DV = DS.Tables[“Employee”].DefaultView;
DV.RowFilter = “IsActive=’true’”;
DV.Sort = “Name asc”;
DropDownList1.DataValueField = “ID”;
DropDownList1.DataTextField = “Name”;
DropDownList1.DataBind();
}
}