在SelectCommand for SqlDataSource中使用选定的下拉值

时间:2013-06-06 20:33:18

标签: asp.net sqldatasource

我想在SqlDataSource SelectCommand中使用下拉列表的选定值。

                <asp:DropDownList ID="ddlSelectRole" runat="server" ClientIDMode="Static">
                    <asp:ListItem></asp:ListItem>
                    <asp:ListItem>Client</asp:ListItem>
                    <asp:ListItem>Programming</asp:ListItem>
                    <asp:ListItem>Guest</asp:ListItem>
                </asp:DropDownList>

目前:

<asp:SqlDataSource ID="dsourceProgEmails" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>" 
    SelectCommand="SELECT  [Email] + ',' AS [text()] FROM [SiteUsers] WHERE [Role] = 'Programming' FOR XML PATH ('')">
</asp:SqlDataSource>

我在想:

<asp:SqlDataSource ID="dsourceProgEmails" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>" 
    SelectCommand="SELECT  [Email] + ',' AS [text()] FROM [SiteUsers] WHERE [Role] = '" + ddlSelectRole.SelectedValue + "' FOR XML PATH ('')">
</asp:SqlDataSource>

代码隐藏是C#。

1 个答案:

答案 0 :(得分:6)

您的查询在SqlDataSource中应如下所示:

SelectCommand="SELECT [Email] + ',' AS [text] FROM [SiteUsers] WHERE [Role] = @RoleID FOR "

然后指定在何处获取RoleID的值

<SelectParameters>
   <asp:ControlParameter Name="RoleID" ControlID="ddlSelectRole" 
        PropertyName="SelectedValue" />
</SelectParameters>