1

I have a drop down list that is populated from a database table:

<asp:DropDownList ID="userNameDropDown" runat="server" DataSourceID="SqlDataSource1"
    DataTextField="Name" DataValueField="PK_User" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    SelectCommand="SELECT [Name], [PK_User] FROM [User] ORDER BY [Name]">
</asp:SqlDataSource>

When I assign a specific row as default it works fine:

userNameDropDown.SelectedIndex = 3;

However when I use this dropdown:

    <asp:DropDownList ID="catagoryDropDownListEdit" runat="server" DataSourceID="SqlDataSource5"
        DataTextField="Catagory" DataValueField="PK_SupportCatagory">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
        SelectCommand="SELECT [PK_SupportCatagory], [Catagory] FROM [SupportCatagory]">
    </asp:SqlDataSource>

And try:

catagoryDropDownListEdit.SelectedIndex = 1;

I get this error:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: 'catagoryDropDownListEdit' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value

Here is a pic that shows there are infact values:

https://i.sstatic.net/akNaf.png

5
  • at which event you are setting its index value Commented Jul 2, 2013 at 16:28
  • It causes the same error everywhere. It's actually when a user clicks 'edit' on a gridview but if I move it to Page_Load the same error occurs. Commented Jul 2, 2013 at 16:29
  • before setting index do dropdownid.databind() Commented Jul 2, 2013 at 16:31
  • databind() worked. Thank you very much. Could you explain why to this noob? Commented Jul 2, 2013 at 16:36
  • See this answer...below...for explanation Commented Jul 2, 2013 at 16:37

1 Answer 1

2

DataBinding Raised after the control's PreRender event, which occurs after the page's PreRender event. (This applies to controls whose DataSourceID property is set declaratively. Otherwise the event happens when you call the control's DataBind method.)

So to set selected inedex of dropdownlist on page load you have to call DataBind() method like this...

dropdownlist1.DataBind()

After this you can set the index.But if you set before binding than it will give error because there no item in dropdownlist.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.