views:

78

answers:

3

I am populating a dropdownlist control from a database. That happes fine, however, when I display the dropdown at runtime, I want the first visible option to be "Please select a faculty member" (which would be option value 0). How can I accomplish that?

                    SqlConnection con = new SqlConnection(strConn);
                string sqlFacultyMember = "usp_GetFacultyMember";
                SqlCommand cmdFacultyMember = new SqlCommand(sqlFacultyMember, con);
                cmdFacultyMember.CommandType = CommandType.StoredProcedure;
                con.Open();

                SqlDataAdapter ada = new SqlDataAdapter(cmdFacultyMember);
                DataSet ds = new DataSet();
                ada.Fill(ds);

                    ddlFacultyMember.DataSource = ds;
                    ddlFacultyMember.DataValueField = "UserID";
                    ddlFacultyMember.DataTextField = "FullName";
                    ddlFacultyMember.DataBind();
A: 

You need to insert a record at position 0 in the DataSet with Value = "0" and Text = "Please Select a Value".

egrunin
+2  A: 

You can also add the item to the DropDownList after you bind it like so:

ddlFacultyMember.Items.Insert(0, new ListItem("Please Select a Value", "0"));

What I like about this approach is it leaves the adding of this to the UI, where I think it belongs, since this is a convenience for the user, not a piece of data you actually want to action on.

CubanX
good answer.....
user279521
A: 

You need to set the AppendDataBoundItems to true for your DropDownList. Also in your selectionindexchanged event check if the selected index is not zero.

if (DropDownList1.SelectedIndex != 0)
        {
 // 
        }
PRR