我试图将变量从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;
});
答案 0 :(得分:1)
这是我从你的问题中收集到的,但我不太确定它是否正确。
你有2个下拉菜单,第一个选择值,第二个使用选择语句中的值来获得你想要的结果......如果是这种情况,那么我建议你不要使用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>
现在为c#,非常简单快捷:
//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();
}
希望这会有所帮助,复制并将其复制到一个新文件中以查看它是否有效,只需确保更改选择语句,因为我刚才使用上述语句作为示例。
答案 1 :(得分:1)
有可能通过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>
不再需要代码。