Using Aspnet Components

You can pass data in a URL by appending name-value pairs to the end of the address. Note the METHOD="GET" for the form variable.

TYPE THIS:

<HEAD><SCRIPT LANGUAGE="C#" RUNAT="Server"> public void Page_Load(Object sender, EventArgs E) { if (IsPostBack) {

labelButtonExample.Text =

Request.QueryString["dropdownlistSuggestions"].ToString(); }

<FONT FACE ="Verdana">

<H3>Welcome to mylifetimegoals.com</H3>

What suggestions do you have for improving our site?

<FORM RUNAT="Server" METHOD="GET">

<ASP:DROPDOWNLIST ID="dropdownlistSuggestions" RUNAT="Server"> <ASP:LISTITEM>Fewer Goals</ASP:LISTITEM> <ASP:LISTITEM>More Goals</ASP:LISTITEM> <ASP:LISTITEM>Same Number of Goals</ASP:LISTITEM> </ASP:DROPDOWNLIST>

<ASP:BUTTON ID="buttonExample" RUNAT="Server" TEXT="Submit"/><P/>

<ASP:LABEL ID="labelButtonExample" RUNAT="Server"/>

RESULT:

A Web page that, when a selection is made and the form is submitted, places the form values into the query string.

Add an if statement to make sure that the code will only run when posting to the page.

|Add the Request.Form method to retrieve the value that was selected in the drop-down list box.

— Save the file and request it from the Web server.

Click 0 and select a suggestion.

Click the Submit button.

-■ The suggestion you chose appears.

DISPLAY DATA WITH REQUEST.PARAMS

rou can use the Request object's Params method to obtain a combined collection of QueryString, Form, ServerVariables, and Cookies items. This will give you most of the data that is in a Web request. Before ASP.NET, this combined collection of the QueryString, Form, ServerVariables, and Cookies items was the default collection of the Request object. This information is still available directly from the Request object, but to be more explicit, you can access through Params.

You can pull request information from any page that requests a Web page and allows server-side code. Therefore, a simple HTML page can be the source for the Request.Params collection. For example, you can pull form data from an HTML form on a simple html page. To do this, you will need to have the HTML form post to the Web page that will process the HTTP request. On this processing page you can use the Request.Params collection to obtain any form data.

If you are not sure of the requesting page's form method, then the Params collection is very useful. The method you use determines if the form data is available in the Form collection (METHOD="POST") or the QueryString collection (METHOD="GET"). Because the Params collection has both of these collections combined, you can just pull the value from Params.

DISPLAY DATA WITH REQUEST.PARAMS

DISPLAY DATA WITH REQUEST.PARAMS

Q Open the

Suggestions.htm template from the Code Templates directory.

LH Add the ACTION value to the form.

■ The contents of the page contain a suggestions form.

^ Untitled - Notepad

-lsl*l

I File Edit Format Help

cHTML=-

<HEAD=-

<FONT FACE ="Verdana"=-

l<H3>Welcome to mvlifetimeqoals.com</H3>l

IIHere is all the data from Request.Params.-eP/> I—i

|<%|GetRequestParams(),|%>| 1

c/FORM> </FONT> </BODY> c/HTMLs

GenericTemplate.aspx template from the Code Templates directory.

0 Add a message to the file.

Q Open the

Suggestions.htm template from the Code Templates directory.

LH Add the ACTION value to the form.

■ The contents of the page contain a suggestions form.

GenericTemplate.aspx template from the Code Templates directory.

Add a heading to the file.

0 Add a message to the file.

_Q Add the script delimiters.

'-Q Call the GetRequestParams function.

USING ASP.NET COMPONENTS

You can just display the form variables using the following code: Example:

<SCRIPT LANGUAGE=MC#" RUNAT="Server"> public void GetRequestForm() { int intCounterl;

NameValueCollection namevaluecollectionForm; namevaluecollectionForm=Request.Form;

String[] stringarrayForml = namevaluecollectionForm.AllKeys;

for (intCounterl = 0; intCounterl < stringarrayForml.Length; intCounter1++) { Response.Write("Form: " + stringarrayForml[intCounterl] + "<BR/>");

"i Untitled Notepad

File Edit Format Help c% @ PAGE LANGUAGE-'C#" cHTMLs

<SCRIPT LANGUAGE-'C#" RUNAT=MSeiYerM> public void GetRequestParams() {

nt intCcunterl, intCcunter2, NameValueCollection namevaluecollectionParams; namevaluecollectionParams - Request.Params, StringO stringarrayParamsl - namevaluecollectionParams.AIIKeys, for ( intCounterl = 0; IntCounterl < stringarrayParamsl .Length; intCounterl++

Response.Write( "Key. " + stringarrayParams1[intCounter1] -

StringO 5trlngarrayParams2 -namevaluecollectionParams.GetValues( stringarrayParamsl [intCounterl] ), for ( intCounter2 = 0; intCounter2 « stringarrayParams2.Length;

Response.Write( "Value " + intCounter2 + ": " +

stringarrayParams2[intCounter2] + "<br>" );

_• Add the GetRequestParams function and code from the file GetRequestParams.aspx located in the Code Templates directory.

| Save the file and request it from the Web server.

— Request the first Web page that you created in this task.

Click 0 and select a suggestion.

Click the Submit button.

-■ Request data displays from looping through the Request.Params collection.

You can use the HTTPResponse class to interact with the responses given to Web requests. One common task for the HTTPResponse is to write custom HTML in the response to a user's page request. Writing custom HTML can be accomplished with the Write method of the Response object. Note that the methods and properties of the HTTPResponse class are exposed through ASP.NET's intrinsic Response object.

To get user input to display in a new HTML element, you can programmatically add HTML elements/ tags with Response.Write. When using the

Response.Write to customize the HTML markup of a page, you need to know where you want to have the tag(s) placed. You can place in a placeholder tag in which you can insert HTML SPAN or DIV tags), or you can use the script delimiters inline to the HTML.

If you are posting a page back to itself, you can check the isPostBack property of the page to determine if it is the first time you are displaying the page or if you have posted back to the same page. In some cases, you will only want to run certain server-side code after a page is posted back to itself.

WRITE OUTPUT USING RESPONSE.WRITE

WRITE OUTPUT USING RESPONSE.WRITE

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

< Add a message to the user.

Place a drop-down list box on the page with suggestions for choosing the suggested number of goals.

□ Add a button control to the page.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

Add a heading for the page.

< Add a message to the user.

Place a drop-down list box on the page with suggestions for choosing the suggested number of goals.

□ Add a button control to the page.

Was this article helpful?

0 0

Post a comment