Link to home
Start Free TrialLog in
Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to utilize the CommandArgument value alongside PostBackUrl functionality??

I have a gridview that displays a list of links.  Each link has a commandArgument (pulled from the database), like so...

<ItemTemplate>
   <asp:linkbutton runat="server" ID="lnk" Text="Click to View" PostBackUrl="~/page2.aspx" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>

I'd like the link to post to a new page (page2.aspx), but yet still have access to the link's commandArgument value in the destination page.  

Is this possible??

I've tried both queryString and Session to pass the value across to page2, but each seem to have their weaknesses.
I know I can use PreviousPage.FindControl in a normal non-gridview situation, but how does it work is this instance??
Avatar of Argblat
Argblat

Try this:

Get rid of the PostBackUrl attribute, and add an onCommand event to the GridView.  In the code behind for this event, add something like Response.Redirect("~/page2.aspx" + e.CommandArgument

It's not going to look exactly like that I don't think, but the idea just might get you what you need.

-Mike
Avatar of Rouchie

ASKER

This forms a URL such as this when the links are clicked:
   http://localhost/solution/page2.aspx6

Previously today I amended the redirection code to say this:
  Response.Redirect("page2.aspx?id=" & e.CommandArgument, True)
which gave a correctly formatted URL, but as I mentioned in the question I want to stay away from querystrings.  This is mainly to hide this sort of stuff from the user.
Is the issue passing the data to the next page, or the way you pass the data to the next page?

You should be aware of the pro's and con's of all the ways that you can pass data from one page to another (querystring, session, etc), weigh your options, and choose the one that's right for the situation.

It was my understanding that the question at hand was how to grab the information to be passed, and that once we accomplished this you could do the analysis and decision making on how to pass the data yourself ... Correct me if I am wrong about this...

Getting the '6' at the end of the querystring accomplished the first task. . . my apologies for forgetting the '?id=' part, but you seem to have figured that out on your own

What is there left to do?

-Mike
Avatar of Rouchie

ASKER

Hi Mike
The problem is the 'way' in which the data is passed.  I know of a few approaches that work that I have tried already:

 1) Querystring - looks untidy and allows for abuse
 2) Session - Works well but I clear out the session once the value is read back in on page2.  This results in errors if page2 is refereshed as the session is cleared.
                   Currently I am using this approach and redirecting back to page1 if the session value is not found
 3) Javascript to populate hidden text box prior to submitting - Lots more work!
 4) Server.Transfer - probably the best I guess but will get confusing if errors creep in!

I had hoped (given the popularity of the gridview) that there'd be an easy way to pass the data under the hood to page2.  That's why I wondered about CommandArgument given that it plays a vital role in all the other approaches.  After reading about this all day though it seems a waste of time because if there was 100 rows in the view, then you'd need to differentiate them.  

Perhaps I'm just looking for something that's over-complicated...!   8-/
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial