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?
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.