0

我目前正在尝试遍历一个数组(两个值)并在循环内的查询中使用这两个值。请看下面的代码。现在我的代码不起作用。我正在尝试在 SQLDataSource 的“SelectParameters”标签中动态填充“appType”参数,但这不起作用。

有什么建议吗?

<%

    Dim appTypes() As String = {"Extranet", "Internet"}

    For Each appType As String In appTypes

%>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ProviderName = "<%$ ConnectionStrings:CMS.ProviderName %>"
    SelectCommand = "SELECT Applications.Name as AppName, Applications.Abbr as AppAbbr, Types.Name as TypeName, Managers.LastName as LastName, Managers.FirstName As FirstName, Managers.EDKeyEmpID as EDKeyID
        FROM Types INNER JOIN (Managers INNER JOIN Applications ON Managers.ID=Applications.Manager) ON Types.ID=Applications.Type
        WHERE (Types.Name = @appType)
           ORDER BY Types.Name, Applications.Name;"
    ConnectionString="<%$ ConnectionStrings:CMS %>">
    <SelectParameters>
        <asp:Parameter DefaultValue="<%=appType%>" Name="appType"  Type="String" />
    </SelectParameters>   
</asp:SqlDataSource>

<asp:Repeater ID="Repeater1" DataSourceID="SqlDataSource1" runat="server">
    <ItemTemplate>
        <%#Eval("AppName")%> (<%=appType%>)
    </ItemTemplate>
</asp:Repeater>


<% Next %>
4

1 回答 1

2

修改您的 SqlDataSource 以拥有一个OnSelecting成员:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" OnSelecting="OnSelecting"

将您的 SelectParameters 重置为正常:

<SelectParameters>
    <asp:Parameter Name="appType" Type="String" />
</SelectParameters>

在您的代码隐藏中定义此方法。根据需要实现逻辑。在这里,我从您的中继器中显示了一个虚拟值。这将由您来确定实际来自的方式/位置(SelectedItem 或类似的东西)。

Protected Sub OnSelecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
    e.Command.Parameters("@appType").Value = Repeater1.SomeValue
End Sub
于 2012-05-11T20:56:45.997 回答