Reputation: 969
I am trying to pass an object from one page to another page using <asp:hyperlink>
without any success. I was trying to invoke a C# method and put that object into a session but then I realized you can't invoke a method using <asp:hyperlink>
. Then I thought about using <asp:linkbutton>
but then I need to open the new webpage in a new window.
How can I go about doing this properly? Are there any other good alternatives?
Upvotes: 2
Views: 7151
Reputation: 47726
Then I thought about using
<asp:linkbutton>
but then I need to open the new webpage in a new window.
You do not need to open a new window... add this to your server side LinkButton
handler:
<asp:LinkButton id="btnYourLinkButton" runat="server"
OnClick="btnYourLinkButton_Click">Test</asp:LinkButton>
protected void btnLogout_Click(object sender, System.EventArgs e)
{
var someObject = GetYourDataWithSomeFunction();
Session["YourData"] = someObject; // saves to session
Response.Redirect("yourNewUrl.aspx");
}
This will store the value in the Session
and redirect to a new page in the same window.
EDIT:
If you need to open in a new window then do the same thing as outlined above but instead of doing a Response.Redirect
add the window.open
javascript call to your page that is served up to open the new window:
ScriptManager.RegisterStartupScript(this, this.GetType(), "AUTOOPEN",
"window.open('yourNewUrl.aspx', '_blank');", true);
Optionally you could just add an ajax call to your click method to setup the Session
server side and then trigger the redirect based on your ajax call complete.
Upvotes: 5
Reputation: 5277
Any web application tends to be stateless in nature. Your objects only live during the processing of the page request. When developing and appliction with a technology such as ASP.Net the general pattern for object retrieval is to send an identifier as part of the form post data or the querystring and then use this identifier to reload the object that you were working with prior to the previous page post/request.
It is possible to add objects to the session and retrieve them as suggested in other answers here, but there are issues with this approach e.g. sessions timing out, scalability etc.
If you were to give some more details as to the nature of what you are trying to do it would be easier to give you a more complete answer or suggestions on how to solve your particular problem.
Upvotes: 1
Reputation: 25684
Add the object to the Session
then redirect to the new page. In the new page, check the Session
variable for the object.
Upvotes: 3