Work With Html Controls

When you viewed the source, you may have noticed that there was another hidden variable on the form that you did not create. This hidden variable is used for state information for controls on the form. ASP.NET automatically creates this variable.

If you ever want to programmatically remove an input tag from the page that the user receives, you can set the Visibility property equal to false. This will remove the tag from the response to the user, even though the .aspx file has an <input> tag marked up in the document.

httpij'/localhost/HTMLInputHidden.aspH Microsoft Internet Explorer

View I Favorites Tools Help

? Personal Bar Search lüg Favorites 3 | UV ¿j I—| E

iputHidden.aspx jj fr>Go I Link:

Welcome to mylifetimegoals.com

What suggestions do you have for improving our site?

¡More security..

ffiDoi

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

Note: See pages 20 to 25 for instructions on saving a file to the Web server and then requesting the file using the IIS Admin.

0 Type in a suggestion.

-Q Click the Submit button.

_Q Click View O Source from the menu in Internet Explorer.

Q HTMLInpulHidden[1] Notepad

File Edit Format Help

Hhtml>

<FONT FACE-'Verdana">

<H3-Welcome to myiifetimegoals com</H3> What suggestions do you have for improving our site?

<form name-'ctri2" method-'post' action-'HTMLInputHidden aspx" id-"ctrl2"> <input type-'hidden" name-"_VIEWSTATE"

value="dDwtMTQwNzl3NDY3Mzs7PkaVCnjuB/BFLnjBbllCoWgSMIVc,7> <p/>

<input name="inputSuggestions" id="inputSuggestions" type-Text" size="40" value-'More security.."

<inputname-'hiddenSuggestions' d-'hiddenSuggestions' type-'Hidden" value-'More security..." />

<input name-'ctrl7" type-'Submit" value-'Submit' A=

<SPAN d="spanMessage"></SPAN> </form>

■ The source for the page appears and the hiddenSuggestions tag contains the suggestion.

UPLOAD FILES

People who visit your Web sites can upload files to your Web server using the HTMLInputFile control. They can choose a local file, and then have that file uploaded to the Web server. For some applications, having the user upload a file of a predetermined format is a more convenient way of inputting data, as opposed to having the user type all of the information into a form.

To be able to send a file, you are required to modify the HTMLForm control to contain the attribute name/value pair of ENCTYPE="multipart/ form-data". This attribute instructs the Web browser that one of the controls is a file that needs to be uploaded to the server. On the form, you can then add an HTMLInputFile control by using the <input> tag and setting the attribute's

TYPE="File" and RUNAT="Server".

After a user selects a file by using the HTMLInputFile control, the user will submit the form to the server, sending the file up with the HTTP request. To place the file on the server's file system, you need to write some code that will check for the file, save the file if available, and do some exception handling if there is a problem.

UPLOAD FILES

UPLOAD FILES

Q Open the

SuggestionsTemplate.aspx template from the Code Templates directory.

0 Add the ENCTYPE attribute and specify that the form will be posting multipart form data.

In the SubmitBtn_Click function, check to make sure there is a filename entered.

□ Catch the exception if the post was unsuccessful and set the message to reflect an error.

Q Open the

SuggestionsTemplate.aspx template from the Code Templates directory.

0 Add the ENCTYPE attribute and specify that the form will be posting multipart form data.

Add a message and an HTMLInputFile control to the form.

In the SubmitBtn_Click function, check to make sure there is a filename entered.

^0 Post the file up to the Web server and set the message for the <SPAN> tag.

□ Catch the exception if the post was unsuccessful and set the message to reflect an error.

WORK WITH HTML CONTROLS

Because there is a chance that users could overwrite files on the Web server when uploading files, you may want to make sure the names of files are unique. One way of doing this is by using the System. Guid class. The following modifications to the SubmitBtn_Click function would save files to the Temporary directory using a GUID, which stands for a Globally Unique Identifier. See the complete source code on the CD-ROM, Chapter04\Code\HTMLInputFile_ai.aspz.

<SCRIPT LANGUAGE="C#" RUNAT="Server"> void SubmitBtn_Click(object Source, EventArgs e) { if (fileSuggestions.PostedFile != null) {

string stringFileName = System.Guid.NewGuid().ToString();

fileSuggestions.PostedFile.SaveAs("c:\\temp\\" + stringFileName + ".txt"); spanMessage.InnerHtml = "File uploaded successfully to " + "c:\\temp\\" + stringFileName + ".txt";

catch (Exception exc) {

spanMessage.InnerHtml = "Error saving file" +

"c:\\temp\\" + stringFileName + ".txt" + exc.ToString();

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

■ The Choose file dialog box appears.

■ The path and filename appear in the text box.

■ A message appears notifying you that the file successfully uploaded to the Web server.

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

Note: See pages 20 to 25 for instructions on saving a file to the Web server and then requesting the file using the IIS Admin.

Q Click the Browse button.

■ The Choose file dialog box appears.

Select a file to upload to the Web server.

LE Click Open.

■ The path and filename appear in the text box.

Click the Submit button.

■ A message appears notifying you that the file successfully uploaded to the Web server.

Was this article helpful?

0 0

Post a comment