DavidJGu's Blog

       尼采说,宁可追求虚无也不能无所追求!  Keeping is harder than winning
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

A small Class for simplifying the Work with URL Parameters(转)

Posted on 2004-12-26 17:16  Let's DotNet  阅读(427)  评论(0编辑  收藏  举报

By Uwe Keim
from http://www.codeproject.com/useritems/QueryString.asp

Example 1 - Manipulate an attribute value

Create a QueryString and simply let itself fill with the parameters of the current page:

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    QueryString qs = new QueryString();
 
    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];
  
    // Write a value into the QueryString object.
    qs["name1"] = "This is a value";
  
    // Redirect with the current content of the QueryString object.
    Response.Redirect( qs.All, true );
}

As you can see, you can simply modify a parameter's value by assigning it to the appropriate parameter name by using the [] operator. The All property returns the complete URL that is stored inside the QueryString object including the latest (possibly modified) parameters.

Example 2 - Removing parameters

To remove a certain parameter from the a QueryString object, call the RemoveParameter method, specifying the name of the parameter to remove:

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    QueryString qs = new QueryString();
 
    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];
  
    // Now remove the parameter.
    qs.RemoveParameter( "name1" );
   
    // This has the same effect as RemoveParameter() method:
    qs["name1"] = null;
  
    // ... Further processing of the value1 variable ... 
}