【问题标题】:How to pass two parameters in query string?如何在查询字符串中传递两个参数?
【发布时间】:2013-09-29 16:04:24
【问题描述】:

我想通过查询字符串传递两个参数cartid 和productis。 Cartid 将从会话(如果可用)生成,否则从数据库生成,产品将从先前的查询字符串中获取

我的代码是(如果要从数据库中获取购物车 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);

                }

但两个查询都是错误的 正在生成这样的网址

http://localhost:1030/SShopping%20Website/client/ViewCartItems.aspx?CartID=id%20&%20ProductID=productid

请帮忙

【问题讨论】:

    标签: asp.net sql-server-2008 c#-4.0 query-string


    【解决方案1】:

    使用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 方法。如果您指定 true 对于 endResponse 参数,此方法调用 End 方法 引发 ThreadAbortException 异常的原始请求 当它完成时。此异常对 Web 有不利影响 应用程序性能,这就是为什么为 建议使用 endResponse 参数。

    阅读:Response.Redirect

    【讨论】:

      【解决方案2】:

      您需要将值连接到字符串中:

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

      【讨论】:

        【解决方案3】:

        您在'&', 'variable name' , '=' 之间放置空格。

        不要放空间。像这样写:&name=,而不像 & name =

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

        这会起作用。

        【讨论】:

          猜你喜欢
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多