2

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?

0

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have a list and depending on what they click, I want a page related to that list item opened in a new window. I would prefer it opened in a new window otherwise I would have easily used linkbutton and session to help accomplish this.
@Mowgli I have added to my response that should force the open of a new window.
3

Add the object to the Session then redirect to the new page. In the new page, check the Session variable for the object.

2 Comments

Sounds simple enought but I need to invoke a method and make sure I am putting the right object in the session. How can I invoke a method and still open the new page in a new window. I am trying to do this without having to use javascript.
You can invoke the method by passing a query string parameter, then in the OnLoad event on the new page, check for the parameter, and invoke the method if its present.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.