Sunday, October 22, 2006

Uploading files to your website

ASP.NET2.0 introduces a new file upload control!!!

<asp:fileupload id="FileUpload1" runat="server"></asp:fileupload>



The new control exists under the standard tab





Dragging this form appears as follows on the form designer






The properties of this new control is listed below




The actual work is done by some simple lines of C# code, it assumes that there is an uploads folder with the correct permissions to write files into!


String savePath = Request.PhysicalApplicationPath;
savePath += "uploads\";

if(FileUpload1.HasFile()){
--savePath += FileUpload1.FileName;
--FileUpload1.SaveAs(savePath);
}


Please note that SaveAs will cause an existing file with the same name to be overwritten.

Programatially we can limit the size of an upload by doing the following:

if(FileUpload1.PostedFile.ContentLength <= 3145728){
---- //save file
} else {
---- //cancel upload as its greater than 3mb
}

Client scripts on a page

Ever need to put an alert onto a webpage from the codebehind? It wasn't logical doing it within ASP.NET1.x but now it seems better.

eg. on the page load

String script = "alert('testing this functionality');";
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "MyKey", script, True);

The RegisterClientScriptBlock() method has changed namespaces between 1.x and 2.0. It is now under the ClientScriptManager class (you can get access to this using Page.ClientScript)

The last parameter of this method is the "AddScript" parameter. It indicates whether the script should be enclosed within a <script> block. If this is set to false it will effectively be written to the page and not executed (it will appear as text on the rendered page)

The "key" parameter in 1.0 was allowed to be epty(but not null) now in 2.0 if it is empty it will generate a runtime error.


If you have a javascript include with your script (without the script tags) then use the Page.ClientScript.RegisterClientScriptInclude method it will generate code as follows:

<script src="./includedfile.js" type="text/javascript" ></script>

Validation groups on a page

In ASP.NET1.x controls on a single page are validated together! If you had 2 buttons on a page that were technically not related, example is a page that has a "microsoft search" button and a "save cart" button, then if there was a RequiredFieldValidator on a textbox related to the "save cart" button clicking "google search" will also be invalidated as both buttons are part of the same invalidation group.

ASP.NET2.0 allows controls in a page to be grouped logically so that postback in one group is not dependent on another!


Each button will now have a new attribute called ValidationGroup

The RequireFieldValidator will also have this attribute which signifies what group it belongs to!

A new feature of the RequireFieldValidator is the SetFocusOnError attribute. It will set the focus of the page to the ControlToValidate when the error occurs!


asp:requiredfieldvalidator id="RequiredFieldValidator1" errormessage="Required Field Validator Error" ControlToValidate="txtAge" ValidationGroup="groupOfControls" SetFocusOnError="True"

Focus of controls

In asp.net 2.0 you can now set the focus of a control using its Focus() method. The Focus() method works on the server side, therefore be careful as postbacks will occur!

----txtAge.Focus()

If you want client side focusing then the following link will help you out!


Setting the default button and default focus on a Web Form is also simple now

----<form id="form1" runat="server" defaultbutton="btnSubmit" defaultfocus="txtAge">