改变查询字符串参数/值 [英] altering QueryString parameters / values

查看:225
本文介绍了改变查询字符串参数/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解新的参数设置为URL的问题
并通过

I would like to better understand the issue of setting new parameters to a url and retrieve it via

VAR ParaValue =的Request.QueryString [参数名称];

所以,如果我有一个URL:HTTP://www.myWebsite.aspx用户名=爱丽丝

so if I have a URL : "http://www.myWebsite.aspx?UserName=Alice"

我将通过例如检索它上面

I will retrieve it via example above

字符串的uname =的Request.QueryString [用户名]的ToString();

但如果我想改变例如价值使用户名=拉尔夫

but what if I want to alter value e.g. make UserName = "Ralf"


  • 重新修改


有一个参数国家的持有提及至极按钮是pressed
国家的价值=无​​
现在我想将它设置为img_button1。

when a button is pressed , there is a parameter "state" that holds a reference to wich button was pressed the value of state was = "none" now i want to set it to img_button1.

我没有连送实际工作imgbutton ID

i am not even sending the actuall imgbutton id

我努力的编码只是为了测试/参照

i am hard coding it just for testing /referencing

所以我可以知道我在事件中受的特定事件的procidure reaquested舞台
BUTTON1

so i could know i am in the stage of event reaquested by the procidure of the given event of button1

img_button2

我然后对子级要设置的状态为img_button2

i whould then want to set the state to "img_button2" etc'

推荐答案

我做了我的研究(我不能在这里标记好心给我的任何职位的答案)后,
然后,我测试了我在这个堆栈溢出网页遇到了两个选项:

after I have made my research (I couldn't mark any answer kindly given here on my post) then I tested two options I've encountered in this Stack Overflow Page:

第一个选项(艾哈迈德Mageed给)我已经测试工作得很好。
和可读性容易理解(因为我仍然记忆犹新到asp.net'招数')

first option (given by Ahmad Mageed) I have tested to work just fine . and readability was easy to understand (as I am still fresh to asp.net 'tricks')

再其次答案由
在路上annakata这是显着改善的办法,你
实际上没有重定向到实现的结果 - 查询字符串修改

then followed the answer by annakata which was remarkably improved approach in the way that you don't actually have to redirect to achieve result - Query String IS modified

玩耍后,我已经desided遵循 annakata 的做法
并做出使用也是一个redirerion选择一个辅助方法
改性查询字符串参数&安培;值。

after playing around i have desided to follow annakatas approach and make a helper method that was using also a redirerion option with modified QueryString Parameters & values.

public void QuerStrModify(string CurrQS_ParamName, string NewQs_paramName, string NewPar_Value, bool redirectWithNewQuerySettings = false)
{

    // reflect to readonly property 
    PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

    // make collection editable 
    isReadOnly.SetValue(this.Request.QueryString, false, null);

    // remove 
    this.Request.QueryString.Remove(CurrQS_ParamName);

    // modify 
    this.Request.QueryString.Set(NewQs_paramName, NewPar_Value);

    // make collection readonly again 
    isReadOnly.SetValue(this.Request.QueryString, true, null);
    string FullUrl = Request.Url.AbsolutePath;
    if (redirectWithNewQuerySettings)
    {
        Response.Redirect(string.Join("?", FullUrl, this.Request.QueryString));
    }

}

我觉得这是有人有asp.net的developmet相当少的经验非常有帮助
所以我张贴它作为我的版本的正确答案,因为我看到了。
我希望这会帮助别人somoeone寻求相同的解决方案。

i find it very helpful to someone that has Considerably less experience with asp.net developmet so i posted it as my version of correct answer , as i see it . i hope it'll help somoeone else that seeks the same Solution.

随意进一步完善它,因为我mentiond我不是一个成熟的人才..yet。

feel free to further improve it , as i mentiond I'm not a proven talent ..yet.

这篇关于改变查询字符串参数/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆