ASP.net not posting back to correct page

2019-09-08 11:20发布

So I have a slightly unorthodox application type.

I have an aspx page called AddNewBlog.aspx. This page generates XML data from database queries and it include the file AddNewBlogXSL.aspx which is an xsl style sheet. The effect is that the AddNewBlog XML data is transformed by AddNewBlogXSL on the client side into XHTML.

So although the requested page is AddNewBlog.aspx, the layouts and controls and forms are on AddNewBlogXSL.aspx since it contains all the layout and formatting.
When on AddNewBlogXSL.aspx I do an asp:button it tries to post back to AddNewBlogXSL.aspx as is understandable.

The problem is that page is an xslt stylesheet not a webpage.. I need it to post back to AddNewBlog.aspx as this is the proper page which includes AddNewBlogXSL.aspx

The only thing I seem to be able to do is allow the default behaviour which is to submit to AddNewBlogXSL.aspx, process the page, and redirect them to the proper page AddNewBlog.aspx but then it makes it hard to handle error messages and such since I have no control over AddNewBlog.aspx after I have simply redirected to it from AddNewBlogXSL.aspx

Any ideas at all?

2条回答
一纸荒年 Trace。
2楼-- · 2019-09-08 11:36

You are looking for PostBackUrl property.

<asp:button id="Button2"
  text="Post value to another page" 
  postbackurl="~/Path/To/AddNewBlog.aspx" 
  runat="Server">
</asp:button>

EDIT:

To address your comment, IsPostBack will not be true in this scenario because it isn't a postback, it's just a post to another page. You have to access the values via the Page.PreviousPage property as outlined in the MSDN article I listed.

During a cross-page postback, the contents of the source page's controls are posted to the target page, and the browser executes an HTTP POST operation (not a GET operation). However, in the target page, the IsPostBack property is false immediately after a cross-page post. Although the behavior is that of a POST, the cross-posting is not a postback to the target page. Therefore, IsPostBack is set to false and the target page can go through its first-time code.

Also per MSDN, you would check the PreviousPage.IsCrossPagePostBack property instead of the Page.IsPostBackProperty

if(PreviousPage.IsCrossPagePostBack == true)
{
     //Get values from PreviousPage
    text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;
}

Cross Page Posting Details

I went ahead and wrote a little test harness (aka, I took the example off the MSDN page, :-0 )to verify and results are as follows when cross page posting:

Test Results It's not an ideal situation and it kludgey to access your values as listed, but for the model you have designed, it's the best I can think of.

查看更多
倾城 Initia
3楼-- · 2019-09-08 11:39

In addition to the above answer, Can you please confirm that the "AutoEventWireUp" is false in this page. If so, override the page load method in this case.

查看更多
登录 后发表回答