How to add query string after url when asp.net button is clicked

I have an asp.net page that has a button on it. When you click on it, I want a request to appear after the usual URL, for example, id = 1. How can I do this from the C # server side?

+4
source share
4 answers

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

You can redirect your button's event handler to a location using the query string ...

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

You can display the button as LinkButton and set the url ...

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

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

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

I prefer the latter method, especially when I need a whole bunch of links.

By the way, this is a KISS application - all you need is a regular old link, you do not need to jump through hoops on the server side to create a link with a request in it. With regular HTML client code, whenever possible, you can simply save ASP.Net. I do not see enough of this technique in the wild.

+10
source

I understand that this is an old question, but I had the same problem when we wanted to create a new URL string so that Google Analytics could โ€œcountโ€ when the form was submitted.

I used the ASP: button to submit the form and save to the database and display a thank you note for postback. So the pre- and post-submit url was the same, and I had to change it somehow.

I used the following code and it worked for me:

C # in Page_Load:

 btnSubmit.PostBackUrl = "Page.aspx?id=" + id.ToString() + "&newquerystring=newvalue"; 

where .aspx Page is the page with the button that I want to send back (on the same page), ID is the dynamic identifier that I use to grab content from our CMS and, obviously, a new request element.

Hope this helps.

+4
source

There are several ways to add a request to url. You can use the following code. If you want to add a server side value:

 protected void Button_Click(object sender, EventArgs e) { Int32 id = 1; // Or your logic to generate id string url = String.Format("anypage.aspx?id={0}",id.ToString()); } 
+1
source

How to build a query string for a url in C #?

Or you can use this code.

 string url = Request.Url.GetLeftPart(UriPartial.Path); url += (Request.QueryString.ToString() == "" ) ? "?pagenum=1" : "?" + Request.QueryString.ToString() + "&pagenum=1"; 
+1
source

Source: https://habr.com/ru/post/1479408/


All Articles