Wednesday, March 7, 2012

Changing Parameter in SQLDataSource for Multiple DropDowns

I have one page, one connection, and three dropdowns.
The connection looks like (as an example):

<asp:SqlDataSource ID="DropDownConn" runat="server" ConnectionString="<%$ ConnectionStrings:aousConnectionString %>"
SelectCommand="SELECT [Value], [Text] FROM [DropDown] WHERE (([Group] = @.Group) AND ([Viewable] = @.Viewable))">
<SelectParameters>
<asp:Parameter Name="Group" Type="String" />
<asp:Parameter DefaultValue="True" Name="Viewable" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>

The DropDowns Look like this:

<asp:DropDownList ID="DropDown1" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDown2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDown3" runat="server"></asp:DropDownList
The C# Code I am trying is like this:
DropDownConn.SelectParameters["Group"].Equals("DropDown1");
DropDownConn.SelectParameters["Viewable"].Equals(true);
DropDown1.DataSourceID = "DropDownConn";
DropDown1.DataTextField = "Text";
DropDown1.DataValueField = "Value";
DropDown1.DataBind();

As an example. I can not get it done so that I don't have to create 3 dataconnections. Any help, PLEASE?

It should be done this way..

DropDownConn.SelectParameters["Group"].DefaultValue = "some group value"; // change it accordingly on what data should be displayed in the dropdownlists
DropDownConn.SelectParameters["Viewable"].DefaultValue = "True";

DropDown1.DataSource = DropDownConn;
DropDown1.DataTextField = "Text";
DropDown1.DataValueField = "Value";
DropDown1.DataBind();

// for second

DropDownConn.SelectParameters["Group"].DefaultValue = "some group value for second dropdown"; // change it accordingly on what data should be displayed in the dropdownlists
DropDownConn.SelectParameters["Viewable"].DefaultValue = "True";

DropDown2.DataSource = DropDownConn;
DropDown2.DataTextField = "Text";
DropDown2.DataValueField = "Value";
DropDown2.DataBind();

-----

You can also, refactor the code to set the datasource for the dropdownlist.

Thanks

-Mark post(s) as "Answer" that helped you

|||

Thank you for the answer, and that method does work. Is that really the way we are suppose to do it. The default value is such a poor naming scheme and Microsoft had done such a good job with most other things. Thanks for the the help.

No comments:

Post a Comment