I followed the directions from MSDN on transferring data between asp.net pages.
But when using the 'PreviousPage' property to access the previous pages controls, I get a null ref exception that PreviousPage is not set to an instance of an object.
Here is my code:
public partial class Portal : System.Web.UI.Page
{
public string Username
{
get
{
return txt_User.Text;
}
}
And this is the submit button on initial page:
<asp:Button ID="btn_Submit" runat="server" onclick="btn_Submit_Click"
PostBackUrl="~/Query.aspx"
Previous page property on second page:
protected void Page_Load(object sender, EventArgs e)
{
Username = PreviousPage.Username;
}
As per MSDN instructions I also added this at the top of the second pages markup file:
<%@ PreviousPageType VirtualPath="~/Portal.aspx" %>
Also note I have tried Server.Transfer to switch pages instead and that produces the same error.
EDIT, h开发者_StackOverflow社区ere is using Server.Transfer on the initial page click event:
protected void btn_Submit_Click(object sender, EventArgs e)
{
Server.Transfer("Query.aspx");
}
EDIT, button code without event handler:
<asp:Button ID="btn_Submit" runat="server"
PostBackUrl="~/Query.aspx"
style="height: 26px" Text="Submit" />
This works fine for me. If PreviousPage is null, that generally indicates the current page was not displayed as a result of a cross-page postback.
Can you confirm the error is being raised on the second page?
Also, what the onclick="btn_Submit_Click"
in your button definition? There should be no code responding to the click event on the original page. Remember, it will be handled by the target page.
EDIT: Now that you've updated your question, my last point seems to be the issue. You are doing a server transfer from the original page. This is NOT a cross-page postback and that's why PreviousPage is null.
Remove the onclick attribute from your button and delete btn_Submit_Click.
精彩评论