passing more then one value with the querystring w

2020-04-18 05:08发布

问题:

I have a datalist that I want to list the Products that are comming from the Querystring. It works like this: Default.aspx/?ProductID=1 I get 1 product like I want. But I want to add more products like this Default.aspx/?ProductID=1,15,25 and get three products back. How do I make that to work?

<asp:DataList ID="DataList1" runat="server">
    <ItemStyle VerticalAlign="Top" />
    <ItemTemplate>
        <a href="../Product/Default.aspx?ProductID=<%#Eval("ProductID") %>">
            <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>' />
            <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price") %>' />
        </a>
    </ItemTemplate>
</asp:DataList>


  protected void Page_Load(object sender, EventArgs e)
{
    string id = Request.QueryString["ProductID"];

    DataTable table = CategoryAccess.GetList(id);

    list.DataSource = table;
    list.DataBind();     
}


ALTER PROCEDURE GetList
@ProductID INT

AS
SELECT ProductID, Name, Price 
FROM Product
WHERE (ProductID = @ProductID)

回答1:

You can have:

/page.aspx?ProductID=1&ProductID=15&ProductID=25

Then Request.Querystring("ProductID") will return 1,15,25

This can then be put into an ArrayList like Request.Querystring("ProductID").split(",")

Passing those 3 values as a parameter is a bit more tricky though, youre probably best passing it as xml:

Passing multiple values for one SQL parameter