1

I'm trying to pass variable from jQuery to SqlDataSource in dropdownlist.

First, the jQuery's variable get the value from asp:dropdownlist_1 then keep it in the variable in jquery.

Next step, I would like to send the jquery's variable to SelectCommand in dropdownlist_2. Is it possible ?

If you have a good idea, it will be a great honor if you share it to me.

Thank you in advance

This is my ASPX markup:

<asp:DropDownList ID="DropDown_1" runat="server" CssClass="form-control" 
     DataSourceID="SqlDataSource_1" 
     DataTextField="field_1" DataValueField="field_2"></asp:DropDownList>                               
<asp:SqlDataSource ID="SqlDataSource_1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:asrsDBConnectionString %>" 
     SelectCommand="SELECT * FROM [table_1]"></asp:SqlDataSource>

<asp:DropDownList ID="DropDown_2" runat="server" CssClass="form-control" 
     DataSourceID="SqlDataSource_2" DataTextField="field_1" 
     DataValueField="field_2"></asp:DropDownList>                               
<asp:SqlDataSource ID="SqlDataSource_2"unat="server" 
     ConnectionString="<%$ ConnectionStrings:asrsDBConnectionString %>" 
     SelectCommand="SELECT * FROM [table_2] WHERE [variable_from_jquery] = "value_JQ"></asp:SqlDataSource>

This is my Javascript code:

$('#<%:DropDown_1.ClientID %>').change(function () {
  var value_JQ = $('#<%:DropDown_1.ClientID%>').val();
  return value_JQ;
});
1
  • You can reference the drop down directly (without jquery) by using control parameters. see this. If you must use a javascript variable then set the value on a hidden field and then use that as the control parameter per the example linked to. Commented Sep 30, 2017 at 10:47

2 Answers 2

1

It is potentially possible to set some (hidden) input via jQuery and then use it.
But there is no need in such sophisticated approach. Apply KISS principle (Keep It Simple Stupid). Everything can be done in .aspx declaratively. Something like this.

<%--AutoPostBack="true" is important here --%>
<asp:DropDownList runat="server" ID="lstCountry" DataSourceID="sqlCountry" 
    DataTextField="CountryName" DataValueField="CountryId" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource runat="server" ConnectionString='<%$ConnectionStrings:MyCNN %>' ID="sqlCountry"
    SelectCommand="select CountryId,CountryName from dbo.Countries" />

<asp:DropDownList runat="server" ID="lstState" DataSourceID="sqlState" 
    DataTextField="StateName" DataValueField="StateId">
</asp:DropDownList>
<asp:SqlDataSource runat="server" ConnectionString='<%$ConnectionStrings:MyCNN %>' ID="sqlState"
    SelectCommand="select StateId,StateName from dbo.States where CountryId=@CountryId" >
    <SelectParameters>
        <%--Take parameter value from 1st dropdown --%>
        <asp:ControlParameter Name="CountryId" ControlID="lstCountry" 
            PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

No more code required.

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

3 Comments

Your method is worked, but can we avoid page's reloading ?
Sure you can. Wrap the code provided by asp:UpdatePanel.
Thank you so much Alex. You saved my day !!
1

This is what I have gathered from your question, but I am not too sure if it is correct.

You have 2 drop down menus, the first selects the value, and the second uses that value in a select statment to get the results you want... If this is the case then I suggest that you don't use jquery, and just use C# and asp.net. Also there is a really useful DropDorwnList event called OnSelectedIndexChanged which can be used to do this really easily.

So this is how it would work, first the aspx file:

<!-- For the first drop down menu, as you have done above, creating a SqlDataSource with a SelectCommand that will get the data from your table (I am using a table called products as an example) -->
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:asrsDBConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:asrsDBConnectionString.ProviderName %>"
    SelectCommand = "Select * from products">
</asp:SqlDataSource>

<!-- For the first drop DropDownList make sure you create a DataValueField of the column you want to show as the values form your DropDownList. Also pay attention to the name of the OnSelectedIndexChanged which will be very important for the C# part. And remember to set AutoPostBack to true... like the code block bellow -->
<asp:DropDownList ID="DropDownList1" runat="server"
    DataSourceID="SqlDataSource1"
    DataValueField="ProductID" 
    OnSelectedIndexChanged ="DropDownList1_SelectedIndexChanged"
    AutoPostBack="true">
</asp:DropDownList>


<!-- Now for the second DropDownList create another SqlDataSource, however this time don't create a SelectCommand because it will be created in the C# file depending on what you selected on your first DropDownList-->
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:asrsDBConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:asrsDBConnectionString.ProviderName %>">
</asp:SqlDataSource>

<!-- when creating the second DropDownList make sure the DataValueField is the column that you want as values -->
<asp:DropDownList ID="DropDownList2" runat="server"
    DataSourceID="SqlDataSource2"
    DataValueField="UnitPrice"
    AutoPostBack="true">
</asp:DropDownList>

Now for the c#, very easy and quick:

//create a new method inside your page class, right under the Page_Load if possible, so inside the page class but outside the Page_Load method. The name of the method is what you set the OnSelectedIndexChanged event to from the first dropdown list. Hence, DropDownList1_SelectedIndexChanged
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //get the selected value like so:
    string item1 = this.DropDownList1.SelectedValue.ToString();

    //and now set the SelectCommand for SqlDataSource which is used for the 2nd dropdownlist and use the value to filter your table.
    SqlDataSource2.SelectCommand = "Select * from products where ProductID='" + item1 + "'";
    //DataBind is important!
    SqlDataSource2.DataBind();
}

Hope this helps, copy and past it into a new file to see if it works, just make sure to change select statements as I just used the ones above as an example.

1 Comment

Glad that it did :)

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.