3

I'm filling my dropdownlist from database but I want to add "select an item please" to the dropdown along with others and make it default selected item :
I tried to setwriterddl.SelectedValue = "please...";but a runtime error says:

'writerddl' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

and here is my code:

protected void Page_Load(object sender, EventArgs e)
{
writerddl.SelectedValue = "please...";
if (!IsPostBack)
  {
   writerddl.DataSource = DS.show_all_writers();
   writerddl.DataValueField = "writerid";
   writerddl.DataTextField = "writersname";            
   writerddl.DataBind();
  }
}
Naveen
  • 1,496
  • 1
  • 15
  • 24
Aya Mohammad
  • 245
  • 3
  • 13
  • 1
    Possible duplicate of http://stackoverflow.com/questions/267064/asp-net-add-blank-item-at-top-of-dropdownlist – crthompson Dec 24 '13 at 05:55

3 Answers3

4

Try this code.

protected void Page_Load(object sender, EventArgs e)
{       
   if (!IsPostBack)
   {
       writerddl.DataSource = DS.show_all_writers();
       writerddl.DataValueField = "writerid";
       writerddl.DataTextField = "writersname";            
       writerddl.DataBind();
       writerddl.Items.insert(0, new ListItem("Please select",""));
   }
}
Raghubar
  • 2,768
  • 1
  • 21
  • 31
1

Remove this:

writerddl.SelectedValue = "please...";

and use this:

//....    
writerddl.DataBind();
writerddl.Items.Insert(0, "please...");
//...

Update:

use Clear() method like this:

protected void Page_Load(object sender, EventArgs e)
{  
   writerddl.Items.Clear();
   writerddl.DataSource = DS.show_all_writers();
   writerddl.DataValueField = "writerid";
   writerddl.DataTextField = "writersname";            
   writerddl.DataBind();
   writerddl.Items.Insert(0, "please...");
}
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63
-1

Use the below code. Note the selected-value line moved below and that makes difference. Hope the item with Value "please..." exists in the dropdown.

 protected void Page_Load(object sender, EventArgs e)
    {           
        if (!IsPostBack)
        {
            writerddl.DataSource = DS.show_all_writers();
            writerddl.DataValueField = "writerid";
            writerddl.DataTextField = "writersname";
            writerddl.DataBind();
            writerddl.SelectedValue = "please...";
        }

    }

Update : "Hope the item with Value "please..." exists in the dropdown" changing this line to "Hope the item with Value "please..." exists in the binding source".

Update 1: If the source don't have item "please..." here is the below code. protected void Page_Load(object sender, EventArgs e) {

    if (!IsPostBack)
    {
        writerddl.Items.Add(new ListItem("Select An Item", "please..."));
        writerddl.DataSource = DS.show_all_writers();
        writerddl.DataValueField = "writerid";
        writerddl.DataTextField = "writersname";
        writerddl.DataBind();
        writerddl.SelectedValue = "please...";
    }

}
Naveen
  • 1,496
  • 1
  • 15
  • 24
  • I think the point of the OP's post is that the "please..." does NOT exist in the datasource, but he wants it added. – crthompson Dec 24 '13 at 05:57
  • @paqogomez Is it possible to select an item before binding?. For the first load the code is trying to select before binding. I am trying to point out that. In comment i also added "hope the item with Value "please..." exists in the dropdown". – Naveen Dec 24 '13 at 05:59
  • You are correct in pointing that out, but incorrect in delivering a working solution. – crthompson Dec 24 '13 at 06:02
  • I think you need to write it so that you assume that the value is NOT in the datasource. – crthompson Dec 24 '13 at 06:03