0

I have a dropdown and a gridview.

The gridview datasource is dependent on dropdown.SelectedValue

The dropdown:

   <asp:DropDownList ID="DropDownListLoggedInUser" runat="server" autopostback="True"
                        DataSourceID="SqlDataSource3" DataTextField="Medarbejder" 
                        DataValueField="Medarbejder" 
        onload="DropDownListLoggedInUser_Load">
                    </asp:DropDownList>

It's datasource:

               <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EGWebtidConnectionString %>" 
                    SelectCommand="SELECT [Medarbejder] FROM [Sager] WHERE ([Medarbejder] IS NOT NULL)">
                </asp:SqlDataSource>

The gridview:

 <asp:GridView ID="GridViewSagsoversigt" runat="server" CellPadding="4" ForeColor="#333333"
                        GridLines="None" Style="text-align: left" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                        OnPreRender="GridViewSagsoversigt_PreRender" DataKeyNames="Sagsnr" EnablePersistedSelection="True"
                        SelectedIndex="0" OnSelectedIndexChanged="GridViewSagsoversigt_SelectedIndexChanged">

Its datasource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EGWebtidConnectionString %>"

                    SelectCommand="SELECT Sagsnr, Arbejskort, Adresse, Postnr, [By], Beskrivelse, Bemaerkning, Ansvarlig, Medarbejder FROM Sager WHERE ([Medarbejder] LIKE '%' + @Medarbejder + '%')">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownListLoggedInUser" Name="Medarbejder" 
                                PropertyName="SelectedValue" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>

When the page loads. The Gridview asks for Dropdown.SelectedValue, before Dropdown has fully loaded and set a SelectedValue. So SelectedValue returns "" and the Gridview shows nothing.

When I click on the dropdown and choose a name, it does a postback and it all works as planned.

So how can I make sure, that dropdown loads first and gridview loads second?

2 Answers 2

2

When you give control to the data source controls, you can't explicitly control this. What you can do is explicitly call DataBind() on the gridview after the loading of the dropdown to rebind to the database again. You can also cancel the very first gridview load (if you want to eliminate a database call) by tapping into the Selecting event and setting e.Cancel = true.

I don't know if the ordering of DataSourceControls affects the priority of the execution... I don't know if that is a factor too...

HTH.

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

3 Comments

How do I know when the dropdown has fully loaded and the selectedvalue is not null anymore?
dropdown_load isn't enough. I just tested. The selectedValue is still null at that point
dropdown_databound did the trick. Can't believe i missed your idea. Thanks
-1

At Page_Load() method

if (Page.IsPostBack){
    //make the GridView to ask the Dropdown.SelectedValue here
}

1 Comment

At that point, the Dropdown still has not loaded and therefor the selected value is null..

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.