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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With