Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add query parameter to URL on PostBack?

Posted on 2009-07-14
9
Medium Priority
?
1,259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 600 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 600 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 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