Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting SelectedValue of a data bound DropDownList

I have an asp.net dropDownList which is automatically bound to a sqlDataSource to values of client type on page load. On page load I am also creating a Client object, one of it's properties is ClientType. I am trying to set the SelectedValue of the ddl according to the value of the ClientType property of the Client object unsuccessfully. I recieve the following error message "System.ArgumentOutOfRangeException: 'ddlClientType' has a SelectedValue which is invalid because it does not exist in the list of items". I understand that this is because the list has not yet been populated when I'm trying to set the selected value. Is there a way of overcoming this problem? Thank you!

like image 561
Dov Miller Avatar asked Jul 19 '11 11:07

Dov Miller


People also ask

How do you bind a dropdown value?

Data binding can be achieved by using the bind-Value attribute and it supports string, int, Enum and bool types. If component value has been changed, it will affect all the places where you bind the variable for the bind-value attribute.

How do you display a selected value in a drop-down list?

STEP 1 − Create a select tag with multiple options and assign an id to the select tag. STEP 2 − Also, create an empty DOM with an id to display the output. STEP 3 − Let there be a button element for the user to click and see the option selected. STEP 4 − Let the user select an option from the dropdown list.

How do I add a default item to DropDownList?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

Which control is used for a DropDownList?

ArrayList object, that contains the items to display in the control. Then, use the Control. DataBind method to bind the data source to the DropDownList control. Use the SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control.


2 Answers

You have to use the DataBound Event, it will be fired, once databinding is complete

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    // You need to set the Selected value here...
}

If you really want to see the value in the Page load event, then call the DataBind() method before setting the value...

protected void Page_Load(object sender, EventArgs e)
{
    DropdownList1.DataBind();
    DropdownList1.SelectedValue = "Value";
}
like image 157
Muhammad Akhtar Avatar answered Oct 19 '22 22:10

Muhammad Akhtar


Before setting a selected value check whether item is in list and than select it by index

<asp:DropDownList id="dropDownList"
                    AutoPostBack="True"
                    OnDataBound="OnListDataBound"
                    runat="server />
protected void OnListDataBound(object sender, EventArgs e) 
{
    int itemIndex = dropDownList.Items.IndexOf(itemToSelect);
    if (itemIndex >= 0)
    {
      dropDownList.SelectedItemIndex = itemIndex;
    }
}

EDIT: Added...

If you are doing binding stuff in Page Load, try to follow this way:

  • Move all binding related code in overriden DataBind() method
  • In Page_Load of Page add: (in case of control do not call DataBind directrly, this is a responsibility of a parent page)
if (!IsPostBack)
{
   Page.DataBind(); // only for pages
}
like image 35
sll Avatar answered Oct 20 '22 00:10

sll