2012-03-12 34 views
1

我想为我做一个下拉列表工作。用户可以在很多'Field1'中,所以我希望下拉菜单显示这些Field1。 目前发生的情况是我刚刚得到一个空白列表。下面是代码的样本:使用@parameter时,下拉列表显示为空

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
        SelectCommand="SELECT DISTINCT Field1 FROM table 
            WHERE (Field3= @Field3)"> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 

如果我删除选择参数和WHERE子句中出现的所有地区保健委员会的名单 - 它只是似乎与@字段3的问题。

编辑:我已经测试在SQL Server中的查询和它的作品对我来说(通过与已知值替换@字段3)

有什么建议?我对asp.net非常陌生,在.cs文件中除了一些授权等外,几乎没有任何内容,所以如果人们可以指出我的方向是正确的,那就太棒了。

编辑2:它可能会帮助,如果我给你的整个模板:

<asp:TemplateField HeaderText="Field1" SortExpression="Field2"> 
      <EditItemTemplate> 
       <asp:Label ID="Label2" runat="server" Text='<%# Eval("Field2") %>'></asp:Label> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="Field1"> 
       </asp:DropDownList> 
       <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
        SelectCommand="Field1 FROM table 
            WHERE (Field3 = @Field3)"> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 
      </ItemTemplate> 
     </asp:TemplateField> 

回答

0

你填充使用DROPDOWNLIST了selectedValue作为参数,另一个控制,由你的代码决定。在这种情况下 你在controlParameter

<SelectParameters> 
    <asp:ControlParameter ControlID="DropDownList1" Property="SelectedValue" Name="Field3" Type="String" /> 
</SelectParameters> 

丢失财产,如果你想填充DropDownList1你完全脱轨。在这种情况下,你需要显示更多的标记代码和细节。

更新:您不能从您填充的DropDownList控件取参数值。删除参数或使用其他控件提供值

+0

对不起 - 我没有意识到我遗漏了部分下拉代码。我发布了其余的 – 2012-03-12 03:58:00

+0

你想填充DropDownList1?那么你不能从同一个控件获取参数值。 – Mubarek 2012-03-12 03:59:19

+0

我明白了 - 所以我需要从其他地方获取参数值?我有一个用户名的界限,但除此之外,我没有看到我可以从哪里得到它。我需要做一个新的控制吗? – 2012-03-12 19:42:08