MRK
MRK

Reputation: 27

Get the id of a clicked Html link and use it as parameter for an SqlDataSource in C#

I have a large Database and a lot of 'let's say' names and I have a definitions for these names in another table, so I add an html link between all the names like this:

<a href="Test.aspx" target="_blank" id="2384">John</a>
<a href="Test.aspx" target="_blank" id="2334">Smith</a>

and a lot of these names, when a link is clicked a new tab will open so, in the Test.aspx I have GridVew bounded with an SqlDataSource, what I want is to take id from the link that been clicked a pass it to the SqlDataSource parameter like this:

SqlDataSourceTest.SelectParameters["IdFromAnchor"].DefaultValue = theClickedLinkID

So how could I pass the ID to the Parameter o what should I use, Thanks.

Upvotes: 1

Views: 1458

Answers (1)

Mathew Thompson
Mathew Thompson

Reputation: 56429

You're best off just putting the IDs as a query string parameter and then reading in that parameter on the next page. Make sure you sanitize it though, as you could be open to SQL injections:

<a href="Test.aspx?id=2384" target="_blank" id="2384">John</a>
<a href="Test.aspx?id=2334" target="_blank" id="2334">Smith</a>

Then just do:

Request.QueryString["id"];

Beware though, that can be easily changed by users to any id they please, so if you don't want users to see things with id's other than what they can see, you'd have to perform additional checks there.

Upvotes: 4

Related Questions