Solved

How to add query parameter to URL on PostBack?

Posted on 2009-07-14
9
1,205 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

14 Experts available now in Live!

Get 1:1 Help Now