473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ASP.NET ---- How to get database value as dropdownlist selected value

Hi All,

I have an editable datagrid with 10 columns, one of those is ddl column which I used for showing the status....

[ Please note : Status is a template column with an item template as label... and edit item template as dropdownlist]

By default all the columns are readonly, when user clicks edit, status ll be enabled and im getting the values in the grid.......

say, following are the values in status

Beginner
Intermediate
Expert

If i update my status as Expert it should show Expert in my next edit, but dropdownlist always takes first value ..... Can any one help me how to fetch the database value as dropdownlist selected value......


Thanks a lot in Advance ......
Nov 25 '08 #1
1 10427
dmj07
55
Attach a databound event to your dropdownlist on .aspx page e.g.

<asp:DropDownList ID="ddl" runat="server" OnDataBound="ddl_DataBound" />

Then in code behind retrieve value from database and make it selected value on drop down list e.g.

protected void ddl_DataBound(object sender, EventArgs e)
{
//Connect to SQL and run select statement to retrieve value
//i.e. SELECT Status FROM Table WHERE......

SqlDataReader rdr = cmdUser.ExecuteReader();

while (rdr.Read())
{
ddl.ClearSelection();
ddl.Items.FindByText(rdr.GetString(0)).Selected = true;
}
rdr.Close();
}
Nov 27 '08 #2

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.