交叉时不显示目标aspx页面

时间:2013-03-12 11:07:25

标签: c# asp.net webforms cross-page-posting

我是ASP.NET新手。 我正在尝试使用webforms。我有2个页面:NewOrder.aspx捕获用户的订单,然后交叉到SaveOrder.aspx,我想保存订单并将一些信息显示给用户。

 <telerik:RadButton ID="BtnSubmirOrder" runat="server" ButtonType="StandardButton" AutoPostBack="true"
                Text="Place order" PostBackUrl="SaveOrder.aspx">
 </telerik:RadButton>

加载SaveOrder.aspx时,下面的代码设置其控制值并保存订单。但是浏览器停留在NewOrder.aspx。

protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage==null || !PreviousPage.IsCrossPagePostBack)
            Response.Redirect("~/Default.aspx");

        var referencingPage = PreviousPage as NewOrder;
        int id = Int32.Parse(referencingPage.SelectedPublicationId);


        DateTime neededBy = referencingPage.SelectedOrderDate;

        LblSummaryIsbn.Text = referencingPage.SelectedIsbn;
        LblSummaryNbrCopies.Text = referencingPage.NbrOfOrderedCopies;
        DateTime orderDate = DateTime.Now;
        LblSummaryOrderDate.Text = orderDate.ToShortDateString();
        LblSummaryTitle.Text = referencingPage.SelectedPublicationTitle;
        int quantity = Int32.Parse(referencingPage.NbrOfOrderedCopies);
        StockContainer _context = new StockContainer();
        Order newOrder = Order.CreateOrder(orderDate, quantity, neededBy, id);
        _context.Orders.AddObject(newOrder);
        _context.SaveChanges();
    }

我可以从javascript看到单击按钮时表单的动作设置为SaveOrder.aspx,为什么浏览器不显示它?

1 个答案:

答案 0 :(得分:0)

我认为您的浏览器停留在NewOrder.aspx上的原因是您没有与按钮关联的按钮点击事件。没有看到你的其余代码,我不确定为什么表单的动作看起来不同。

您可以执行以下操作来介绍按钮点击事件。重定向逻辑可以进入那里。

<强>标记:

<telerik:RadButton ID="btnSubmit" runat="server" Text="Place Order" 
OnClick="btnSubmit_Click" />

代码:

public class NameOfPage
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // You could do validation here and display an error message if something is not right.
        // For simplicity I am assuming the data comes from a set of textboxes.
        if (!PageIsValid())
        {
            return;
        }

        StockContainer _context = new StockContainer();
        Order newOrder = Order.CreateOrder(txtOrderDate.Text, txtQuantity.Text, txtNeededBy.Text, id);
        _context.Orders.AddObject(newOrder);
        _context.SaveChanges();
        // Add your redirect logic here.
    }

    private bool PageIsValid()
    {
        if (string.IsNullOrEmpty(txtOrderDate.Text))
        {
            return false;
        }

        if (string.IsNullOrEmpty(txtQuantity.Text))
        {
            return false;
        }

        // and so on for the other fields that are required.
        return true;
    }
}