Solved

How to add query parameter to URL on PostBack?

Posted on 2009-07-14
9
1,219 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 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