Solved

How to add query parameter to URL on PostBack?

Posted on 2009-07-14
9
1,212 Views
Last Modified: 2013-11-08
I have an OnClick event on a button that posts back. I want to inject a parameter in the URL during the postback so it shows up in the URL when the postback finishes. How can I do this?
0
Comment
Question by:bemara57
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:prairiedog
ID: 24851387
Response.Redirectnewpage.aspx?id=queryid&title=titletext");

You can change "id" and "title" to your query parameters' names.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24851400
That strange character should be a double quote ". Don't know what happened there.
0
 

Author Comment

by:bemara57
ID: 24851423
I did not want to do that because I wanted the page to still PostBack. This will give me a fresh page.

I want to preserve everything that was posted, but just add a parameter to the URL.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Expert Comment

by:ppittle
ID: 24851424
You can use Response.Redirect to redirect the user to a new URL.  So something like this:

string newURL = Request.URL.PathAndQuery;
string newQueryStringParameter = "myProp=myVal";
//Add the new query string - be sure to check if a query string already exists;
if (newURL .Contains("?"))
{
//newURL already contains a QueryString, so append the new parameter at the end
newURL += "&" + newQueryStringParameter;
}
else
{
//newURL does not already have a QueryString
newURL += "?" + newQueryStringParameter;
}

//redirect the client
Response.Redirect(newURL);
0
 
LVL 8

Assisted Solution

by:ppittle
ppittle earned 150 total points
ID: 24851439
Bemara57,

What you're trying to do wont work.  You can't modify the URL the client sends.  You can only instruct them (via a HTTP status code of 3xx) to request a different location.  

What is it that you are trying to do where you want to add a QueryString parameter, but maintain the Post Back data?
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 150 total points
ID: 24851462
When you post a parameter in the URL on postback, web server will think it is a new request and will refresh the page. Period.

What are you trying to accomplish? Maybe there is another way to do.
0
 

Author Comment

by:bemara57
ID: 24851500
I have a page that creates an object from the a url parameter, which identifies the id of the object to retrieve from the database. If there is no parameter that exists, then it assumes it is a new creation instead of an update and adjusts the page and validations accordingly.

When the onclick saves, I want the page to refresh with the new object by adding the id to the parameter list, so the page updates off that parameter id. But I still want a postback because I want the page to have a "Successful" message appear from the postback (or a failed message).
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 200 total points
ID: 24851672
why not use a property or static variable on the page...
e.g. public static string id;
on buttonClick set the value of this property and on postBack get the value of this variable to query your DB.
0
 

Author Comment

by:bemara57
ID: 24852555
Instead of a static variable, I ended up using the ViewState. Thanks for the help!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question