Search code examples
c#asp.netpostbackdropdownlist.selectedvalue

ASP.NET DropDownList.SelectedValue gets lost i!nPostBack


This Issue is solved, see my comment below the post

I have a problem with my code that is puzzeling me: I have an ASP.NET DropDownList (simple, not in a gridview or so...) that is statically data bound to a database query. Additionally, I add one list item in the markup:

<asp:DropDownList ID="ddDeviceSelector" runat="server" DataSourceID="CryringDB_InventoryAssets" DataTextField="Identifier" DataValueField="RowID" AutoPostBack="True" OnTextChanged="ddDeviceSelector_IndexChanged" ToolTip="Select a device by name" AppendDataBoundItems="True" Width="300px">
    <asp:ListItem>-- Select Device OR Enter Data of New Device --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="CryringDB_InventoryAssets" runat="server" connectionString="<%$ ConnectionStrings:CMSValuesExplicitUser %>" SelectCommand="SELECT valueColumn, textColumns FROM myTable"></asp:SqlDataSource>

I can call the page with a URL query parameter "?rid=..." where rid contains the value of one specific item in the list. When I try to resolve the rid and select the item in the list by value, the DropDownList.SelectedValue does not get set. Code of Page_Load:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Code to interpret the parameter rid (RowID) in the URL
        // provides functionality to open formAssetDetails of an individual object.
        if (!IsPostBack)
        {
            ddDeviceSelector.DataBind();
            setUiPropertiesByMode(classConstants.displayMode);

            String rowID = null;
            rowID = Request.QueryString["rid"];

            if (rowID != null)
            {
                setUiPropertiesByMode(classConstants.editMode);
                ddDeviceSelector.Enabled = true;
                ddDeviceSelector.Enabled = true;
                ddDeviceSelector.SelectedValue = rowID;
            }
        }

The code executes without problem, but I see an error message in the debugger (breakpoint after line "ddDeviceSelector.SelectedValue = rowID;")

error CS0103: The name 'SelectedValue' does not exist in the current context

(this message is translated from German and might be sightly different in English)

Any help appreciated.


Solution

  • Try setting the selected value in the DataBound event of the dropdownlist which will ensure that your dropdownlist is fully databound with data before you try to set the selectedvalue, remember to delegate this method also:

    //In your designer file
    ddDeviceSelector.DataBound += new System.EventHandler(ddDeviceSelector_DataBound);
    
    protected void ddDeviceSelector_DataBound(object sender, EventArgs e)
    {
        String rowID = null;
        rowID = Request.QueryString["rid"];
    
        if (rowID != null)
        {
            setUiPropertiesByMode(classConstants.editMode);
            ddDeviceSelector.Enabled = true;
            ddDeviceSelector.Enabled = true;
            ddDeviceSelector.SelectedValue = rowID;
        }
    }