2011-03-07 65 views
1

我有一個數據列表,我想列出來自Querystring的產品。它的工作原理是這樣的:Default.aspx/?ProductID=1 我得到1我喜歡的產品。 但是我想添加更多這樣的產品,例如Default.aspx/?ProductID=1,15,25 並獲得三款產品。我如何使它工作?傳遞多個值與具有相同編號的查詢字符串

<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

使用查詢字符串時不要忘記它們是字符串。如果我請求,應用程序會做什麼?Default.aspx /?ProductID = Nicklas – SWeko 2011-03-07 10:27:49

+0

爲了簡短起見,我將代碼縮短了,並且更簡潔。如果數量dosent與數據庫匹配,將出現一個帶有錯誤消息的標籤。 – Nicklas 2011-03-07 10:31:48

回答

2

您可以:

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

然後Request.Querystring("ProductID")將返回1,15,25

這可以被放入ArrayListRequest.Querystring("ProductID").split(",")

傳遞的3個值作爲參數是雖然有點棘手,你可能最好將它作爲XML傳遞:

Passing multiple values for one SQL parameter

相關問題