tags:

views:

68

answers:

4

Hello.

I now have two asp.net pages. When I click on a link on page A, I open the other one (let's call it page B). When I do this, I want to send some information from Page A to Page B in the form of an array of strings. This array is different depending on what link I follow on Page A.

I know I could send this information via the URL with the ?string1=bla1&string2=bla2 etc., but I don't want it that way, as it can get complicated if there are too many strings in the array.

I think there is something similar, like a POST in PHP, but how would that be in ASP?

Any help would be appreciated. Thanks.

Cheers!

+1  A: 

You could use a session variable or HttpContext like this: http://www.codedigest.com/Articles/ASPNET/76_HttpContext_Object_for_Developers.aspx

k_b
+1  A: 

You could just use session state. You'd populate it in the first page something like this

Page.Session["MyArray"] = new string[] {"one", "two", "three" };

Then retrieve the information in your target page with something like this:

string[] values = Page.Session["MyArray"] as string[];

Edit:

In response to your comment, you'd need to handle the OnClick event of your link buttons e.g.

<asp:LinkButton ID="myButton" Text="Click Me" OnClick="myButton_Click" runat="server"/>

then in your code behind, you'd need to set the session state as above e.g.

protected void myButton_Click(object sender, EventArgs e)
{
    Page.Session["MyArray"] = new string[] {"one", "two", "three" };
    Response.Redirect("~/AnotherPage.aspx");
}
mdresser
That array depends on the link that was clicked. So I would need to set the array on the hyperlink's "click" method, but it doesn't have one. How else should I go about this?
Andrei
Hi Andrei, see my edits above.
mdresser
Hi. Thanks for the reply. I tried to do linkButton.Click += new Eventhandler etc., but the even I attach to the link button does not respond when I click on that button...
Andrei
You shouldn't need to bother with setting up event handlers in your code-behind like that (unless you're using .Net 1.1), just use the OnClick attribute in your aspx page like I've done above.
mdresser
Does this work for you though? Cause I'm trying it on my machine and when I click on the link it doesn't pick up the onclick event on the server.
Andrei
Yes, it does work for me. Do you have JavaScript turned off in your browser by any chance?
mdresser
+2  A: 

Just send the values in the URL. That's likely the most correct way to do it anyways. Information that specifies what to display on the page should go in the URL. Pack them into a comma separated string if you don't like going string1=&string2=.

Please don't abuse the session for this, unless you have an absurdly large amount of data to pass. In that case, store them in a dictionary in the session with a unique key and pass that in the URL. This avoids many unexpected problems with slow connections and tabbed browsing and crawlers and whatnot.

Matti Virkkunen
Gee, thanks for the downvote. Could you also tell me why I deserved that?
Matti Virkkunen
Hmm, I would agree with you for most cases, but in case the application decides to use URL masking, it might be tricky.. And no, I wasn't one to vote you down.
Srikanth Venugopalan
i downvoted you, because I skimmed through your comment and saw that you are telling me to do it the way I said I don't want to do it in the description....but now I'm sorry I gave you the downvote, because I actually used your unique key solution.I now put all the information into a dictionary with a unique key, and pass just the key in the URL to the other page. It's a very elegant solution.Thanks!
Andrei
A: 

If we are talking about pure link (a href) tag, there is no other option than putting it in querystring.

If you want to hack it about, you can capture the onclick event of those links and redirect them to your own event handler that will compose whatever you need to say... a hidden field and call the form post when the click event happens.

If it is a postback sort of thing, you can use Session as suggested in the other answer and do a redirect to the other page and pick up the Session from there.

Jimmy Chandra
Thanks, but hyperlinks in ASP.NET apparently don't have click events. How would I go about linking the click event to a method?
Andrei
Do it from the client side. Javascript / jQuery comes to mind...my jQuery is a bit rusty, but this script should be equivalent for a single link tag...document.getElementById("#myLink1").onclick = function(e) { /* Do something here to push the necessary data into one or more hidden fields */; document.getElementById("#myForm").submit(); }Assuming the form id is myForm and the link id is myLink1 and the form method is POST
Jimmy Chandra
oops.. you don't need the # in front of the id :)
Jimmy Chandra