Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a querystring after url when asp.net button is clicked

I have a asp.net page that has a button to click. When click on it, I wish to have a querystring such as ?id=1 added after the normal url. How can I do that from server side c# code?

like image 946
HanYi Zhang Avatar asked Dec 04 '22 11:12

HanYi Zhang


1 Answers

Three ways... server-side redirect, LinkButton, and client-side button or link.

You can have your button event handler redirect to a location with a querystring...

 Response.Redirect("myPage.aspx?id=" + myId.toString(), true);

You can render the button as a LinkButton and set the URL...

 LinkButton myLinkButton = new LinkButton("myPage.aspx?id=" + myId.toString(), true);

Or, you can render the button as a client side link - this is what I do when using Repeater controls...

 <a href='myPage.aspx?id=<%# Eval("myID") %>'>Link</a>

I prefer the last method, particularly when I need a whole bunch of links.

BTW, this is application of KISS - all you need is a regular old link, you don't need to jump through server-side hoops to create a link with a querystring in it. Using regular client-side HTML whenever possible is how to keep ASP.Net simple. I don't see enough of that technique in the wild.

like image 179
Jasmine Avatar answered Jan 17 '23 20:01

Jasmine