Click here to Skip to main content
15,881,709 members
Articles / Web Development / ASP.NET

Declarative QueryString Parameter Binding

Rate me:
Please Sign up or sign in to vote.
4.89/5 (78 votes)
2 Mar 200410 min read 222.6K   1.1K   95  
Describes using reflection to automatically populate member parameters from the Form and Querystring.
using System;

namespace piers7.Web.Controls
{
	/// <summary>
	/// A <see cref="WebParameterAttribute"/> that's specifically bound to the
	/// a parameter in the query string (Request.QueryString collection)
	/// </summary>
	[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
	public sealed class QueryParameterAttribute : WebParameterAttribute
	{
		#region Constructors
		/// <summary>
		/// Creates a new QueryParameterAttribute to load a field from an identically-named
		/// parameter in the QueryString collection, if it exists.
		/// The parameter has no default value, and is not required
		/// </summary>
		public QueryParameterAttribute() {
		}

		/// <summary>
		/// Creates a new QueryParameterAttribute to load a field from the given parameter
		/// in the QueryString collection, if it exists.
		/// The parameter has no default value, and is not required
		/// </summary>
		/// <param name="paramName">The key of a parameter in the QueryString collections</param>
		public QueryParameterAttribute(string paramName) : base(paramName) {
		}
		#endregion
		
		/// <summary>
		/// Retrieves an item from the QueryString by key
		/// </summary>
		protected override string GetValue(string paramName, System.Web.HttpRequest request)
		{
			return request.QueryString[paramName];
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
There's some kinda mutex between money and the time to enjoy it, and it's called work.

Comments and Discussions