Click here to Skip to main content
15,895,746 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 224.1K   95  
Describes using reflection to automatically populate member parameters from the Form and Querystring.
<%@ Page language="c#" Codebehind="WebParameterDemo.aspx.cs" AutoEventWireup="false" Inherits="WebControlTestWeb.WebParameterDemo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>BoundParameterDemo</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body>
		<form id="BoundParameterDemo" method="get" runat="server">
			<H1>QueryString parameter binding demo</H1>
			<P>Using the WebParameter attribute, the <STRONG>FirstName</STRONG> and&nbsp;<STRONG>CustomerID</STRONG>
				fields in the underlying page class are 'bound' to <STRONG>txtFirstName</STRONG>
				and&nbsp;<STRONG>txtCustomerID</STRONG> in the querystring. Additionally the 
				property <STRONG>LastName</STRONG> is bound to <STRONG>txtLast_Name</STRONG> in 
				the querystring.</P>
			<P>In Page_Load the&nbsp;fields/properties are loaded from the querystring, 
				and&nbsp;the two fields&nbsp;values&nbsp;and the property value are 
				assigned&nbsp;to the controls.
			</P>
			<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
				<TR>
					<TD>First Name</TD>
					<TD>
						<INPUT type="text" runat="server" id="txtFirstName"></TD>
				</TR>
				<TR>
					<TD>Last Name</TD>
					<TD>
						<INPUT id="txtLastName" type="text" runat="server"></TD>
				</TR>
				<TR>
					<TD>Customer ID</TD>
					<TD>
						<INPUT id="txtCustomerID" type="text" runat="server"></TD>
				</TR>
				<TR>
					<TD></TD>
					<TD align="right">
						<INPUT type="submit" value="Submit"></TD>
				</TR>
			</TABLE>
			<P>I should stress that this <EM>isn't</EM> advocating an alternative mechanism for 
				loading data into controls after postback - this rather contrived demo is 
				purely to demonstrate that the concept works. A far more&nbsp;common use would 
				be to handle a (non postback)&nbsp;query request like (for example):</P>
			<P>http://somesite/showAccounts.aspx?accountBalanceMin=120&amp;StartDate=2003/01/01&amp;EndDate=2004/01/01</P>
			<P>&nbsp;</P>
		</form>
	</body>
</HTML>

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