views:

93

answers:

2

Using C# & Java Script

I have the link like this

"http://localhost/Server/Vehicle/Vehicle.aspx?appid=5", when i use this link the page is opening... But i want to get this appid value, then pass this appid value to another link

In the above link appid value is 5

For Example

Link1 http://localhost/Server/Vehicle/Vehicle.aspx?appid=5

In link2 the value 5 should display like this "http://localhost/Server/Vehicle/car.aspx?appid=5"

Tried Code

<a href="car.aspx?param=document.getElementById('appid').value">Entry</a>

But in another page the link is displaying like this

http://localhost/Server/Vehicle/car.aspx?param=document.getElementById('appid').value

How to get that appid value. I want to pass this value to another link

Need code Help

+6  A: 

Access the Request.QueryString as follows to retrieve the value of the appid query variable:

string appid = Request.QueryString["appid"];

Update:

The JavaScript snippet won't be executed in the href attribute of a link (it's recognized as a normal string, and won't be parsed as JavaScript code).

With the following link a user will be successfully directed to your desired URL:

<a href="#" onclick="javascript:window.location.href = 'car.aspx?param=' + document.getElementById('appid').value; return false;">Entry</a>

Side note: the value property works only for HTML tags that have defined an eponymous attribute. One such tag would be the input tag. The div tag instead doesn't have a value attribute defined, and therefore document.getElementById('appid').value would fail; use innerHTML instead in that case.

Giu
Mmmm... HttpRequest[...] indexer returns a combined collection, that includes the QueryString. What he has done should have worked. I think there is something else wrong. `http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx`
Strelok
@Strelok That's a good point! Looks like the problem is the context in which he sets the text. Maybe he's using the wrong event to initialize it!? It surely would be helpful to post more of his code in this case.
Giu
@Giu, How to use you code, i tried your code in the href link like this "<a href="javascript:window.location.href = 'car.aspx?param' + document.getElementById('appid').value" onclick="javascript:window.location.href = 'car.aspx?param' + document.getElementById('appid').value">Entry</a>"The link is not working...., No page is displaying. How to solve my issue.
Gopal
@Gopal Just use the HTML snippet I've posted as it is: `<a href="#" onclick="javascript:window.location.href = 'car.aspx?param' + document.getElementById('appid').value; return false;">Entry</a>`
Giu
I tried your code also, is not working, when i click the menu, the page is not redirecting and also link is displaying # symbol at last.....
Gopal
@Gopal I've tested it locally (Opera, IE, Firefox, Chrome), and it works perfectly; it doesn't append the hash symbol to the URL. How are you defining the link? Are you using an `asp:LinkButton`?
Giu
No, using HTML for the link
Gopal
@Gopal Then `<a href="#" onclick="javascript:window.location.href = 'car.aspx?param' + document.getElementById('appid').value; return false;">Entry</a>` should do the work. Could you show me the context in which you *define* the link?
Giu
@Giu, when i change the code like this <a href="car.aspx" onclick="javascript:window.location.href = 'car.aspx?param' + document.getElementById('appid').value; return false;">Entry</a>, Car page is opening without the appid value, when i used your code, car.aspx page is not opening...., Why this issue is coming may be the problem with this code "javascript:window.location.href"
Gopal
@Gopal I see. The `return false;` is there to prevent the default behavior of the link, namely directing you to the URL you define in the `href` attribute when you click it. I've defined `<input type="text" id="appid" value="5" />` and I'm using the snippet I posted, and the redirection is made correctly. Which browser (including the exact version number) are you using?
Giu
@Gopal I've found the bug; I've forgotten to put a `=` after `param` in the newly created URL. I've updated the code snippet in my answer. If I could down vote myself, I would do it right now. Sorry for that! Now it should definitely work :)
Giu
Google Chrome, I tried in internet explorer also, is not working....?
Gopal
@Giu I've updated the snippet in my answer (see my last comment). Did you try it out?
Giu
Yes i tried, is not working....., when i click the link, it is displaying # at last, car.aspx page is not displaying...
Gopal
A: 

You can try using

string appID;
if(Request.QueryString["appid"] != null)
{
   appID = Request.QueryString.Get("appid");
}
Dev
Can you please explain the circumstances in which your code would work, but the posted code wouldn't?
Kobi
If you will access Request.QueryString["appid"] without any check then it might throw an exception.
Dev
@Dev `Request.QueryString` won't throw an exception if you try to get a query variable that doesn't exist. Furthermore, with your code snippet you wouldn't circumvent the throwing of an exception, since you're accessing the `QueryString` yourself with `Request.QueryString["appid"]`
Giu