Home > Forum > ASP.NET > Get value from DB in enable and disable option

Get value from DB in enable and disable option

by Thamil   on Aug 26, 2013   Category: ASP.NET  |  Views: 1330    |  Points: 5   |  Gold 
  Reply
Hi,

Am using asp.net with dropdownlist control.
In my table have some values like One,Two and N/A
I get all values from table and bind to dropdownlist

But i want if my dropdownlist enable status means load all valuea without N/A
if my control disable mode means i want only N/A and same like edit option.

In my edit mode all values disable and get selected values.

Give any samples

Thanks

Question posted on behalf of Anu


Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Get value from DB in enable and disable option   
by Thamil
on Aug 26 2013 9:34AM
Points : 10
Gold 
Hello Anu,

Please try with below way to achieve this.

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = LoadData();
if (DropDownList1.Enabled)
{
DataRow[] dr = dt.Select("Number='N/A'");
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Number";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
else
{
DropDownList1.Items.Clear();
DropDownList1.Items.Insert(0, new ListItem("N/A", string.Empty));
}
}
private DataTable LoadData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Number");

DataRow rw3 = dt.NewRow();
rw3["id"] = "1";
rw3["Number"] = "One";
dt.Rows.Add(rw3);

DataRow rw4 = dt.NewRow();
rw4["id"] = "2";
rw4["Number"] = "Two";
dt.Rows.Add(rw4);

DataRow rw5 = dt.NewRow();
rw5["id"] = "3";
rw5["Number"] = "N/A";
dt.Rows.Add(rw5);

DataRow rw6 = dt.NewRow();
rw6["id"] = "4";
rw6["Number"] = "Four";
dt.Rows.Add(rw6);

return dt;

}

Recent Post

Latest Posts