gpt4 book ai didi

javascript - 如何将变量从 jQuery 传递到 SqlDataSource 中的 SelectCommand?

转载 作者:行者123 更新时间:2023-11-30 15:01:17 25 4
gpt4 key购买 nike

我正在尝试将变量从 jQuery 传递到下拉列表中的 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>

这是我的 Javascript 代码:

$('#<%:DropDown_1.ClientID %>').change(function () {
var value_JQ = $('#<%:DropDown_1.ClientID%>').val();
return value_JQ;
});

最佳答案

有可能通过 jQuery 设置一些(隐藏的)输入,然后使用它。
但是没有必要采用这种复杂的方法。应用 KISS 原则(Keep It Simple Stupid)。一切都可以在 .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>

无需更多代码。

关于javascript - 如何将变量从 jQuery 传递到 SqlDataSource 中的 SelectCommand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com