简体   繁体   中英

Page unable to redirect

public partial class Order : System.Web.UI.Page
{
    private Product SelectedProduct;
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlProducts.DataBind();
        }
        SelectedProduct = this.GetSelectedProduct();
        lblName.Text = SelectedProduct.Name;
        lblShortDescription.Text = SelectedProduct.ShortDescription;
        lblLongDescription.Text = SelectedProduct.LongDescription;
        lblUnitPrice.Text = (SelectedProduct.UnitPrice).ToString("C");
        imgProduct.ImageUrl = "Images/Products/" + SelectedProduct.ImageFile;
    }

    private Product GetSelectedProduct()
    {
        DataView dvProduct = (DataView)AccessDataSource1.Select(DataSourceSelectArguments.Empty);
        dvProduct.RowFilter = "ProductID = '" + ddlProducts.SelectedValue + "'";
        Product Product = new Product();
        Product.ProductID = dvProduct[0]["ProductID"].ToString();
        Product.ProductID = dvProduct[0]["ProductID"].ToString();
        Product.Name = dvProduct[0]["Name"].ToString();
        Product.ShortDescription = dvProduct[0]["ShortDescription"].ToString();
        Product.LongDescription = dvProduct[0]["LongDescription"].ToString();
        Product.UnitPrice = (decimal)dvProduct[0]["UnitPrice"];
        Product.ImageFile = dvProduct[0]["ImageFile"].ToString();
        return Product;
    }

    protected void btnAdd_Click(object sender, System.EventArgs e)
    {
        if (Page.IsValid)
        {
            CartItem CartItem = new CartItem();
            CartItem.Product = SelectedProduct;
            CartItem.Quantity = Convert.ToInt32(txtQuantity.Text);
            this.AddToCart(CartItem);
            Response.Redirect("Cart.aspx");
        }
    }

    private void AddToCart(CartItem CartItem)
    {
        SortedList Cart = GetCart();
        string sProductID = SelectedProduct.ProductID;
        if (Cart.ContainsKey(sProductID))
        {
            CartItem = (CartItem)Cart[sProductID];
            CartItem.Quantity += Convert.ToInt32(txtQuantity.Text);
        }
        else
        {
            Cart.Add(sProductID, CartItem);
        }
    }

    private SortedList GetCart()
    {
        if (Session["Cart"] == null)
        {
            Session.Add("Cart", new SortedList());
        }
        return (SortedList)Session["Cart"];
    }
}

When i hit Add button it doesn't redirect to the other aspx page i have which is Cart.aspx. I think the problem lies in this chunk of code.

protected void btnAdd_Click(object sender, System.EventArgs e)
{
    if (Page.IsValid)
    {
        CartItem CartItem = new CartItem();
        CartItem.Product = SelectedProduct;
        CartItem.Quantity = Convert.ToInt32(txtQuantity.Text);
        this.AddToCart(CartItem);
        Response.Redirect("Cart.aspx");
    }
}

It wouldn't be a chome setting enabling redirection as i'm able to navigate to the cart.aspx page through another button i have on this page though that is hardcoded to the aspx button itself with a postback URL.

It seems that your code is correct.

Make sure that your page is in the same directory. If it is not in the same directory that use it as mentioned in below sample :

Response.Redirect("[Directory Name]/DrugEntry.aspx",true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM