Reputation:
I've got a Databound DropDownList control that is filled with the following query:
<asp:DropDownList ID="ddlSelector" runat="server" DataSourceID="dataSelector"
DataTextField="Description" DataValueField="Description"
OnSelectedIndexChanged="TextBox_TextChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="dataSelector" runat="server"
ConnectionString="<%$ ConnectionStrings:PRODUCTION %>"
SelectCommand="SELECT [ID], [Description] FROM [Status]">
</asp:SqlDataSource>
I have a second Databound DropDownList control who's values need to be selected from the next available Status.ID
.
<asp:DropDownList ID="ddlChangeTo" runat="server" DataSourceID="dataChangeTo"
DataTextField="Description" DataValueField="Description">
</asp:DropDownList>
<asp:SqlDataSource ID="dataChangeTo" runat="server"
ConnectionString="<%$ ConnectionStrings:PRODUCTION %>"
SelectCommand="SELECT [ID], [Description] FROM [Status] WHERE ([Description] < @Description)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlSelector" Name="Description" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The WHERE
clause above does not fit the solution I am after. It is filtering alphabetically.
What I need is a way to get the [ID]
value from ddlSelector
and put all values greater than that ID
into my ddlChangeTo
control.
How do I get the [ID]
value from the ddlSelector
DropDownList control to use in the Databinding query for the ddlChangeTo
DropDownList control?
Upvotes: 0
Views: 140
Reputation:
OK, I found out how to do this, but I did not find the answer online anywhere.
After starring at the code for a bit and trying various ways, it appears that DataTextField
is what is displayed while DataValueField
is the value that is used for comparison.
This may be obvious to those of you who have done this for a long time, but it was not for me.
To get my control to work, I made the following change to the DataValueField
of the ddlSelector
DropDownList control:
<asp:DropDownList ID="ddlSelector" runat="server" DataSourceID="dataSelector"
DataTextField="Description" DataValueField="ID"
OnSelectedIndexChanged="TextBox_TextChanged"
AutoPostBack="True">
</asp:DropDownList>
I had to make a similar change to the second Databound DropDownList control, the SqlDataSource
, and the ControlParameter
Type:
<asp:DropDownList ID="ddlChangeTo" runat="server" DataSourceID="dataChangeTo"
DataTextField="Description" DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="dataChangeTo" runat="server"
ConnectionString="<%$ ConnectionStrings:PRODUCTION %>"
SelectCommand="SELECT [ID], [Description] FROM [Status] WHERE ([ID] > @ID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlSelector" Name="Description" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Such a cool feeling when you figure it out yourself.
I hope others get some use out of this.
Upvotes: 0
Reputation: 377
The easiest way to accomplish this would be to wrap the entire section in a update panel and bind the second dropdown after the first one has changed.
You could then hook up the asp:ControlParameter to a hidden field and have the first dropdown list populate the hidden field in the code behind on autopostback before calling the databind on the second dropdown.
Upvotes: 1