tags:

views:

743

answers:

2

Every time I test the IsPostBack in PageLoad() false is returned whether or not post data is present. My first reaction was to check to see if the runat="server" tag was missing from the form or submit button. However, they were all added and the WriteEmail.aspx page still always returns false for IsPostBack. I have also tried using IsCrossPagePostBack in place of IsPostBack.

ListInstructors.aspx:

<form runat="server" method="post" action="WriteEmail.aspx">
      ...
      <input type="submit" id="writeEmail" value="Write Email" runat="server" />
</form>

WriteEmail.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Response.Redirect("ListInstructors.aspx");
    }
}
+8  A: 

Post != Postback. A postback is when you post back to the same page. The action on your form is posting to a new page.

It looks like all you're doing is using the WriteEmail.aspx page to send a message and then going back to where you just were. You're not even displaying a form to collect the text there. It's a very... Classic ASP-ish... way to handle things.

Instead, put the code you use to send a message in class separate class and if needed put the class in the App_Code folder. Also change the submit button to an <asp:button ... /> Then you can just call it the code from the server's Click event for your button and never leave your ListInstructors.aspx page.


In response to your comment: No. From MSDN:

... make a cross-page request by assigning a page URL to the PostBackUrl property of a button control that implements the IButtonControl interface.

Joel Coehoorn
Should IsCrossPagePostBack return true for Posts across pages?
Only if referenced from the previous page. Take a look-see at this link for a bit more infohttp://www.alexthissen.nl/blogs/main/archive/2006/09/13/beware-the-iscrosspagepostback-property.aspx
Goblyn27
I removed some of the code in my question to make it more readable. There is actually a form in which an Mass Email can be written. Thank you for the advice. However, testing Request["writeEmail"] (the submit button), should work fine or is there a better way. However, I am still curious to know what the IsCrossPagePostBack is used for.
@Goblyn27 Thanks. That cleared things up a bit.
A: 

The IsPostBack is not true because the form is not being submitted from the WriteEmail.aspx page; submitting a form from the same page is what causes a PostBack. If you submitted the form from the WriteEmail.aspx page, it would be a PostBack; as it is, it's just a Post.

You might find this MSDN reference to be useful:

http://msdn.microsoft.com/en-us/library/ms178141.aspx

McWafflestix