A small Class for simplifying the Work with URL Parameters(转)
Posted on 2004-12-26 17:16 Let's DotNet 阅读(429) 评论(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 ... }
浙公网安备 33010602011771号