如何在查询字符串中传递两个参数?

3
我希望通过查询字符串传递两个参数,cartid和productis。如果有session,则cartid将从session中生成,否则将从数据库中生成,并且Product将从之前的查询字符串中获取。
以下是我的代码(如果要从数据库中获取cart id)。
        CartInfo cartinfo = new CartInfo();
        cartinfo.UserName = Session["UserName"].ToString();
        cartinfo.IsOrder = "0";
        cartinfo.CartDate = DateTime.Now;
        int id = new InsertAction().InsertData(cartinfo);
        if (id!=0)
        {
            lblmsg.Text = "Inserted Sucessfully";
            Session["CartID"] = id;
            if (Request.QueryString["ProductID"] != null)
            {
               int productid = int.Parse(Request.QueryString["ProductID"]);
            }
            Response.Redirect("ViewCartItems.aspx?CartID=id & ProductID=productid");

        }

如果要从创建的会话中获取cartid,则需要进行以下操作:

if (Session["CartID"] != null)
        {
            string cartid;
            int productid;
            if (Request.QueryString["ProductID"] != null)
            {
                cartid = Session["CartID"].ToString();
                productid = int.Parse(Request.QueryString["ProductID"]);
                DataSet ds = new AddCartItem().GetCartItem(cartid, productid);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataSet ds1 = new AddCartItem().UpdateCartItem(cartid, productid);

                }

但是这两个查询都是错误的,它们生成的URL如下:
http://localhost:1030/SShopping%20Website/client/ViewCartItems.aspx?CartID=id%20&%20ProductID=productid

请帮忙

3个回答

14

使用 String.Format 通常更易于阅读:

Response.Redirect(String.Format("ViewCartItems.aspx?CartID={0}&ProductID={1}", id, productid));
此外,最好使用 Response.Redirect(url, false) 而不是仅使用 Response.Redirect(url),这样你就不会收到 ThreadAbortException 异常。
来自 MSDN 的说明:
当您在页面处理程序中使用此方法终止一个页面的请求并启动另一个页面的请求时,请将 endResponse 设置为 false,然后调用 CompleteRequest 方法。如果您为 endResponse 参数指定 true,则该方法将调用原始请求的 End 方法,并在完成时抛出 ThreadAbortException 异常。此异常对 Web 应用程序性能产生不良影响,因此建议为 endResponse 参数传递 false。
阅读资料:Response.Redirect

6
你需要把这些值连接成一个字符串:
Response.Redirect("ViewCartItems.aspx?CartID=" + id.ToString() + "&ProductID=" + productid.ToString());

5

您正在'&', '变量名', '='之间添加空格。

不要加空格。应该像这样写:&name=,而不是像这样写:& name =

Response.Redirect("ViewCartItems.aspx?CartID="+id+"&ProductID="+productid);

这将有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接