Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在SqlDataSource中将变量从jQuery传递到SelectCommand?_Javascript_Jquery_Asp.net_Vb.net - Fatal编程技术网

Javascript 如何在SqlDataSource中将变量从jQuery传递到SelectCommand?

Javascript 如何在SqlDataSource中将变量从jQuery传递到SelectCommand?,javascript,jquery,asp.net,vb.net,Javascript,Jquery,Asp.net,Vb.net,我正在尝试将变量从jQuery传递到dropdownlist中的SqlDataSource 首先,jQuery的变量从asp:dropdownlist_1获取值,然后将其保存在jQuery的变量中 下一步,我想将jquery的变量发送到dropdownlist_2中的SelectCommand。可能吗 如果你有一个好主意,如果你能把它分享给我,那将是莫大的荣幸 先谢谢你 这是我的ASPX标记: <asp:DropDownList ID="DropDown_1" runat="server"

我正在尝试将变量从jQuery传递到dropdownlist中的SqlDataSource

首先,jQuery的变量从asp:dropdownlist_1获取值,然后将其保存在jQuery的变量中

下一步,我想将jquery的变量发送到dropdownlist_2中的SelectCommand。可能吗

如果你有一个好主意,如果你能把它分享给我,那将是莫大的荣幸

先谢谢你

这是我的ASPX标记:

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


这是我从你的问题中得出的结论,但我不太确定这是否正确

您有两个下拉菜单,第一个选择值,第二个在选择状态中使用该值以获得所需的结果。。。如果是这种情况,那么我建议您不要使用jquery,而只使用C#和asp.net。此外,还有一个非常有用的DropDorwnList事件,名为OnSelectedIndexChanged,可用于轻松完成此操作

这就是它的工作原理,首先是aspx文件:

<!-- 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>

希望这能有所帮助,将其复制到一个新文件中,看看它是否有效,只需确保更改select语句,因为我刚刚使用了上面的语句作为示例。

有可能通过jQuery设置一些(隐藏)输入,然后使用它。
但没有必要采用如此复杂的方法。应用接吻原则(保持简单愚蠢)。一切都可以在
.aspx
中以声明方式完成。像这样的

<%--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>


无需更多代码。

您可以使用控制参数直接引用下拉列表(无需jquery)。如果必须使用javascript变量,请在隐藏字段上设置值,然后根据链接到的示例将其用作控制参数。您的方法有效,但我们可以避免重新加载页面吗?当然可以。包装
asp:UpdatePanel
提供的代码。非常感谢Alex。你救了我一天!!很高兴它做到了:)
//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();
}
<%--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>