【问题标题】:Replace Query String of a page替换页面的查询字符串
【发布时间】:2015-08-25 05:50:02
【问题描述】:

我想像这样替换我页面的查询字符串-

首先,我通过设置此 URL 来点击菜单栏项移动到此页面- Response.Redirect("SearchtWorkForceReport.aspx?page=Search");

然后我想像这样更改网址-

“SearchtWorkForceReport.aspx?page=Search”到“SearchtWorkForceReport.aspx?page=Edit”复选框更改事件。 我试试这个代码-

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

它会替换查询字符串,但在页面加载时,如果我得到查询字符串应该再次给出之前设置的字符串。

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

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您无法通过编辑Request.QueryString 来编辑页面的查询字符串。您应该重定向到当前页面。使用下面的代码:

     if (Request.RawUrl.Contains("page"))
     {
         Response.Redirect(Request.RawUrl.Replace("Search", "Edit"))
     }
    

    【讨论】:

    • 先生,我尝试了相同的代码,它应该可以工作,但在页面加载时没有得到新的查询字符串,
    • 您的网址是否包含"page""Search"
    • 是的,我的网址看起来像这样 - SearchtWorkForceReport.aspx?page=Search,我想像这样更改 - SearchtWorkForceReport.aspx?page=Edit
    • Abhishek,确保您使用的字符串与您在问题中提供的字符串相同。问题可能与大小写错误有关(String.Replace 和 String.Contains 是区分大小写的方法)。
    【解决方案2】:

    查询字符串由您的客户端提供,更改您的副本服务器端没有任何效果。

    您必须使用新的查询字符串将您的客户端重定向到新的 URL:

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

    【讨论】:

    • 先生,我不想重定向页面,是否可以在不重定向的情况下更改或获取新字符串。
    【解决方案3】:

    根据您的问题,我的理解是您正在尝试更改第二页上复选框更改事件中的查询字符串。 所以在复选框更改事件中编写此代码

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

    在页面加载中检查查询字符串

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

    【讨论】:

    • 先生,我不想重定向整个页面。
    • Request.QueryString["page"]="Edit" 显示错误集合是只读的。
    • 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.
    • 为什么要更改查询字符串?有什么重要原因吗?
    • 在更改查询字符串之前先尝试清除查询字符串
    猜你喜欢
    • 1970-01-01
    • 2014-07-03
    • 2014-11-22
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多