2011-04-07 61 views
0

您好我想在TextBox2的值發生變化時對DropDownList2執行DynamicPopulate,我如何將值傳遞給TextBox2到DynamicPopulate的sql查詢。如何將TextBox值傳遞給DynamicPopulateExtender查詢?

<asp:DropDownList ID="DropDownList2" runat="server" 
      DataSourceID="SqlDataSource1" 
      onselectedindexchanged="DropDownList2_SelectedIndexChanged"> 
     </asp:DropDownList> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:MYDB %>" 
      SelectCommand="SELECT Time FROM Tour WHERE (Date = TextBox2)"> 
     </asp:SqlDataSource> 
     <asp:DynamicPopulateExtender ID="DropDownList2_DynamicPopulateExtender" 
      runat="server" Enabled="True" PopulateTriggerControlID="" 
      TargetControlID="DropDownList2"> 
     </asp:DynamicPopulateExtender> 

回答

1

這段JavaScript代碼添加到您的網頁

<script type="text/javascript"> 
    function UpdateControl(value) { 
    var behavior = $find('dp1'); 
    if (behavior) { 
     behavior.populate(value); 
     } 
    } 
    </script> 

,並使用此修改後的代碼:

<asp:DynamicPopulateExtender ID="DropDownList2_DynamicPopulateExtender" 
     runat="server" Enabled="True" PopulateTriggerControlID="" 
     TargetControlID="DropDownList2" BehaviorID="dp1"> 
    </asp:DynamicPopulateExtender> 

在代碼的VB或C#頁的後面一定要添加屬性

DropDownList2.Attributes.Add("onChange", "UpdateControl3(this.value);") 

這將觸發DynamicPopulateExtender的WebMethod,並將contextKey設置爲va你的下拉菜單。

希望這會有所幫助。

相關問題