2017-09-30 86 views
1

我想從下拉列表中將變量從jQuery傳遞給SqlDataSource。如何將jQuery中的變量傳遞給SqlDataSource中的SelectCommand?

首先,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

您可以通過使用控制參數直接引用下拉列表(不含jQuery)。 [看到這個](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.controlparameter(v = vs.110).aspx#Anchor_7)。如果您必須使用JavaScript變量,請將該值設置爲隱藏字段,然後將其用作鏈接到的示例的控制參數。 – Crowcoder

回答

1

它有可能通過jQuery設置一些(隱藏)輸入,然後使用它。
但是,這種複雜的方法沒有必要。應用KISS原則(保持簡單愚蠢)。一切都可以在.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 [email protected]" > 
    <SelectParameters> 
     <%--Take parameter value from 1st dropdown --%> 
     <asp:ControlParameter Name="CountryId" ControlID="lstCountry" 
      PropertyName="SelectedValue" Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

無需其他代碼。

+0

你的方法已經工作,但我們可以避免頁面重新加載? –

+0

當然可以。將'asp:UpdatePanel'提供的代碼包裝起來。 –

+0

非常感謝Alex。你救了我的一天! –

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(); 
} 

希望這有助於,複製和過去的成一個新的文件,看它是否工作,只要確保改變選擇語句,因爲我剛剛使用上面的例子。

+0

您的方法已運行。謝謝 –

+0

很高興它確實:) – Dalokey

相關問題