4

I want to replace the Query String of my page like this-

firstly I move to this page on clicking on menu bar Items by setting this URL- Response.Redirect("SearchtWorkForceReport.aspx?page=Search");

then I want to change url like this-

"SearchtWorkForceReport.aspx?page=Search" to "SearchtWorkForceReport.aspx?page=Edit" on a check box change event. I try this code-

 string strQueryString = Request.QueryString.ToString();
 if (strQueryString.Contains("page"))
 {
     strQueryString = strQueryString.Replace("Search", "Edit");
 }

and it'll replace the Query String but on page load if I get the query string should give again the previous set string.

type = Request.QueryString["page"].ToString();

3 Answers 3

1

You can't edit query string of the page by editing Request.QueryString. you should redirect to current page. use code below:

 if (Request.RawUrl.Contains("page"))
 {
     Response.Redirect(Request.RawUrl.Replace("Search", "Edit"))
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Sir I try the same code and it should worked but not get the new query string on page load,
Does your url contains "page" and "Search"?
ya, my url look like this - SearchtWorkForceReport.aspx?page=Search and I want to Change like this- SearchtWorkForceReport.aspx?page=Edit
Abhishek, ensure that you have the same string as you provide in your question. Problem could be connected with wrong case (String.Replace and String.Contains are case-sensitive methods).
0

Query strings are provided by your clients, changing your copy server-side does not have any effect.

You have to redirect your client to the new URL with the new query string:

Response.Redirect("SearchtWorkForceReport.aspx?page=Edit");

1 Comment

Sir, I don't want to redirect the page, Is this possible to change or get the new string without redirecting.
0

From your question my understanding is you are trying to change the query string in check box change event on the second page. so write this code in checkbox change event

Response.Redirect("SearchtWorkForceReport.aspx?page=Edit");

and in page load check the querystring

string type = Request.QueryString["page"].ToString();
if(type=="Edit")
{
//what you want to do?
}

7 Comments

Sir, I don't want to redirect whole the page.
Request.QueryString["page"]="Edit" show error Collection is read only.
Changing the query string of the current request is not supported. Using private Reflection to edit some in-memory state will most likely break ASP.NET because it assumes that the query string is immutable. The only way to change the query string is to issue a new request, either by doing a redirect, or by doing a sort of sub-request, such as by making a new HTTP request to the same page but with a different query string.
why do u need to change querystring?any important reason?
try to clear query string first before change your query string
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.