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, here 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.
Related
I'm fairly new to ASP.NET, I've been reading a few questions related to this but I'm still unable to figure out what's wrong with my code, I have a default.aspx page with a menu on top created using a list (ul and li items) and putting the <a href=""> tag to create the links to other pages but after following a link to another page, the Page_Load event fires before leaving the page, I understand this would be the expected behavior with Response.Redirect, but I don't know how to avoid this using tags (if possible), this is the markup I'm using for the Default.aspx page:
<ul id="lista">
<li><strong>Inicio</strong></li>
<li><strong>Item</strong></li>
<li><strong>IK</strong></li>
<li><strong>Acerca de</strong></li>
</ul>
And this is the code behind I have for Page_Load:
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
ExcelUtility excel = new ExcelUtility();
dtDefault = excel.LeerExcel();
gridResults.DataSource = dtDefault;
gridResults.DataBind();
gridResults.VirtualItemCount = dtDefault.Rows.Count;
}
}
Basically, what I want to do is to follow the link to some other page without loading the default page before leaving, hope I make myself clear!
Edit: The root cause of this was having the default <form runat="server"> tag at the beginning of the body section, this was causing the Page_Load event firing again in the same page once the links were being clicked, placing the hyperlinks outside of the form tag did the trick.
Your HTML code should be inside some HTML tag or custom ASP control which contains the attribute runat="server". This is supposed to fire a PostBack request to the server.
In an asp.net forms website I have a button which has a postpack url:
mailBUtton.PostBackUrl = "mailto:..."
As expected a click on the button opens the local e-mail software, but If an other button on the page is clicked after the mailButton was clicked, the onClick event of the second button is ignored and instead of the onClick event, the PostBackUrl of the mailButton is called.
What is the reason for these behavior?
This is the mailButton:
<asp:Button ID="mailButton" runat="server" Text="Neuer Beitrag" ></asp:Button>
and this the second button:
<asp:Button ID="btnSpeichern" runat="server" Text="Speichern" OnClick="btnSpeichern_Click"/>
and this the event in the code behind:
protected void btnSpeichern_Click(object sender, EventArgs e)
{
speichern(false);
}
According to w3schools the PostBackUrl of a button is
A string specifying the URL of the page to post to. Default is an empty string, this causes the page to post back to itself
So when you click the mailButton you're setting the pages PostBackUrl to mailto:.... By clicking on the second button a postback is performed which now calls the mailto: url instead of the original one, where also is no btnSpeichern_Click method.
Instead of using a button and PostBackUrl you could do the following:
<asp:HyperLink runat="server" NavigateUrl="mailto:abc#abc.com" Text="abc#abc.com"></asp:HyperLink>
If you want your link to appear like a button, you can do that in your css.
Here are some more approaches of using mailto.
I am working on a project for school, and this is an extra credit part. I have a project started in VS 2010 using master pages, and what I'm trying to do is get a "Submit" button to redirect people to the "MyAccounts.aspx" page. My current code for the ASP part for the button looks like this:
<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>
I have tried adding in the OnClick option, as well as the OnClientClick option. I have also added this code to the Site.Master.cs file as well as the Transfer.aspx.cs file:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect(Page.ResolveClientUrl("/MyAccounts.aspx"));
}
When I run this and view the project in my browser, the whole thing runs fine, but when I click on the "Submit" button, it just refreshes the current page and does not properly redirect to the MyAccounts page. Anyone have any ideas for me?
You are doing it almost correctly, you just haven't put the correct pieces together. On Transfer.aspx, your button should be:
<asp:Button ID="btnTransfer" OnClick="btnTransfer_Click" runat="server" Text="Submit"/>
and your code behind should be like what #KendrickLamar said:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect("~/MyAccounts.aspx");
}
The OnClick event tells it what to execute on post-back when the users clicks the button. This is in the code-behind for Transfer.aspx, not the site master.
G'day,
I'm using PreviousPage to get values from controls from another page like this:
sPORyear1 = PreviousPage.year1.SelectedItem.Text.ToString();
It works just fine, however I noticed that the events from C# Code Behind (ex. Button click) are not handled properly. I tried making a breakpoint inside the event and it doesn't stop.
After looking at my aspx I noticed this:
AutoEventWireup="true"
When I set that to false, the buttons work properly, but the PreviousPage functionality doesn't work anymore.
The button code:
<asp:Button ID="export" runat="server" onserverclick="export_Click" Text="Export" />
Thanks!
When the AutoEventWireup is set to "False" Change the Page load Handler signature as below for the another page in which you are trying to access the value of previous page
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
Please note that the access modifier should be protected not Private in case of Autoeventwireup = "False" Otherwise you will not be able to capture the OnLoad event
this fixed it
Button On Click event not firing
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");
}
}
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.
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