Solved

How to add query parameter to URL on PostBack?

Posted on 2009-07-14
9
1,199 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now