Reputation: 149
I am creating a web page that has two controls - a GridView and a DropDownList.
The DropDownList list is populated with a list of companies and uses a sql data source(stored procedure). When a user logs in, his username will be passed to the DropDownList's data source as a parameter so he can only see companies he has access to. This is working fine and below is the HTML markup for the DropDownList.
<asp:DropDownList ID="ddlPortfolioCompanies" runat="server" Font-Names="tahoma" Font-Size="8pt" style="position: absolute; top: 50px; left: 735px; height: 18px;" AutoPostBack="True" DataSourceID="DropDownDataSource" DataTextField="Asset_Name" DataValueField="Asset_ID">
</asp:DropDownList>
<asp:SqlDataSource ID="DropDownDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ReportsConnectionString %>" SelectCommand= "usp_Assets" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="User" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
I am setting the default value of the DropDownList in the code behind in page load as follows
DropDownDataSource.SelectParameters["User"].DefaultValue = System.Web.HttpContext.Current.User.Identity.Name;
The GridView also uses a SQL data source(stored procedure) and the 'selected value' from the DropDownLIst is passed as a parameter to GridView's data source.
<asp:GridView ID="GridView1" runat="server" CssClass="gvclass" OnRowUpdating="GridView1_RowUpdating"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="None" Width="65" Height="16px">
<AlternatingRowStyle BackColor="#F7F7DE" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ReportsConnectionString %>"
SelectCommand="EXEC [Portfolio].[dbo].usp_SELECT @ID"
<selectparameters>
<asp:controlparameter name="ID" controlid="ddlPortfolioCompanies" propertyname="SelectedValue"/>
</selectparameters>
Issue: I am testing this and I have access to two companies(say company A and company B). The first time I log in, the DropDownList is populated with company A. However, the GridView control is not populated and is empty. If I change the value of the DropDownList from company A to company B, GridView shows data and if i change the value back to company A, GridView is showing data. I am stumped and have trying to fix this since yesterday.
Any help would be greatly appreciated. Thank you.
Upvotes: 0
Views: 1385
Reputation: 170
Did you try to set the default value of the other control parameter on the same Page Load event ? something like
user = System.Web.HttpContext.Current.User.Identity.Name;
conn = new SqlConnection(ConnectionStrings:NB-ReportsConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(usp_Assets, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("User", user);
companyID = cmd.ExecuteScalar();
con.Close();
SqlDataSource1.SelectParameters("ID").DefaultValue = companyID
I'm sorry about my c#, Im more about Visual Basic
Upvotes: 1