如何在ASP.net模板字段下拉列表中访问选定的值

时间:2015-05-08 20:42:59

标签: c# asp.net gridview sqldatasource templatefield

我有一个网格视图,其中我有两个带有列表框的编辑模板字段。我需要从DropDownList6中获取值并在DropDownList1的SQL连接字符串中使用它。我已经尝试过我在这方面找到的所有东西但却无法实现,如果有人可以帮助我会非常感激。谢谢(:

<EditItemTemplate>
   <asp:DropDownList ID="DropDownList6" runat="server" autopostback="true" >
      <asp:ListItem>Any</asp:ListItem>
      <asp:ListItem>Central</asp:ListItem>
      <asp:ListItem>East</asp:ListItem>
      <asp:ListItem>West</asp:ListItem>
   </asp:DropDownList>
</EditItemTemplate>

<EditItemTemplate>
   <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource4" DataTextField="roomCode" DataValueField="roomCode"  DataKeyName="roomCode" AutoPostBack="true">
   </asp:DropDownList>
</EditItemTemplate>

<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="SELECT roomCode FROM [Room2] WHERE roomID NOT IN (SELECT roomID FROM [BookedRoom]) AND park="  **THIS IS WHERE I NEED THE VALUE** ">
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

这是您使用SQLDataSource ControlParameter as from here

的地方

从这个网站:

 <p><asp:dropdownlist
      id="DropDownList1"
      runat="server"
      autopostback="True">
      <asp:listitem selected="True">Sales Representative</asp:listitem>
      <asp:listitem>Sales Manager</asp:listitem>
      <asp:listitem>Vice President, Sales</asp:listitem>
  </asp:dropdownlist></p>

  <asp:sqldatasource
      id="SqlDataSource1"
      runat="server"
      connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
      selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
      <selectparameters>
          <asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
      </selectparameters>
  </asp:sqldatasource>

  <p><asp:listbox
      id="ListBox1"
      runat="server"
      datasourceid="SqlDataSource1"
      datatextfield="LastName">
  </asp:listbox></p>

请注意,数据源的控件参数与下拉列表相关联。