Tuesday, September 12, 2006

File Upload form in ASP.NET

In the HTML:

[form Method="post" EncType="multipart/form-data" RunAt="Server" ID="Form1"]
[%-- The File upload Html control --%]
Select File To Process
[BR]
[Input ID="MyFile" Type="file" RunAt="Server" NAME="MyFile" style="WIDTH: 376px; HEIGHT: 22px"
size="43"]
[BR]
[%-- A button - when clicked the form is submitted and the
Upload_Click event handler is fired... --%]
[asp:Label id="lblResult" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 120px"
runat="server" Width="312px" Height="80px"][/asp:Label]
[Input Type="submit" Value="Go" OnServerClick="Upload_Click" RunAt="Server" ID="Submit1"
NAME="Submit1"]
[/form]

In the code :

public void Upload_Click(object Sender, EventArgs e)
{
string strPath;
// get the path into variable
strPath = MyFile.Value;
}

Monday, September 04, 2006

Javascript focus and functions with textBox

Set Focus function on server side code (just pass in the string of the control name)

protected void setFocus( String ctrl )
{
string s = "[SCRIPT language='javascript']document.Form1.Editor1_" + ctrl + ".focus();[/SCRIPT]";
Page.RegisterStartupScript( "focus", s );
}

---------------------------------------------------------------------------------------

Function on server side code to assign javascript event to a particular ASP.NET control (Change the details passed to JavaScript function for each call)

txtStartDateDay.Attributes.Add("onkeyup", "SetFocusX('Editor1_txtStartDateDay','Editor1_txtStartDateMonth')");

In the HTML JavaScript function (checks the lenght of the field if two sets focus to the next)

function SetFocusX(oField, oField2)
{
var strText = document.getElementById(oField).value;
if(strText.length == 2)
{
document.getElementById(oField2).focus();
}
}