2

I am new to ASP.NET. I am experimenting with webforms. I have 2 pages: NewOrder.aspx which captures the user's order and then crossposts to SaveOrder.aspx where I want to save the order and display some information back to the user.

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

When SaveOrder.aspx is loaded, the code below sets its control values and saves the order. however the browser stays at 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();
    }

I can see from the javascript that the form's action is set to SaveOrder.aspx when the button is clicked so why is the browser not displaying it?

kfc
  • 291
  • 7
  • 24
  • Don't know about `RadButton` but this should work with regular buttons – nunespascal Mar 12 '13 at 11:16
  • FWIW, its a much more common paradigm in Web Forms to do your order saving in the code behind of NewOrder.aspx, and then maybe on a successful save routine, redirect to a confirmation page. – Graham Mar 12 '13 at 15:56
  • @Graham , I did it this way because i wanted to display some information back to the user after the order is saved. how can i achieve this if i save in NewOrder.aspx? will i need to redirect him to SaveOrder.aspx? I want this page to be accessible only if an order is saved. – kfc Mar 13 '13 at 13:23
  • There's a few ways you can do that. You could set a session variable at the end of he NewOrder.aspx.cs code, and check for that session variable on the confirmation page. Or maybe just use query strings or something, depending on how secure that confirmation page needs to be. – Graham Mar 13 '13 at 13:27
  • Basically in "standard" webforms development, cross-page posting is not a common pattern, and so you should exhaust the more common approaches first. – Graham Mar 13 '13 at 13:28

1 Answers1

0

I think the reason your browser is staying on NewOrder.aspx is that you don't have a button click event associated with your button. Without seeing the rest of your code, I'm not sure why the form's action appears different.

You could do something like the following to introduce a button click event. The redirect logic could go in there.

Markup:

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

Code:

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;
    }
} 
Malice
  • 3,927
  • 1
  • 36
  • 52