Friday, January 1, 2010

Happy New Year

Today is “22”. 01/01/10 or 010110

Happy New Year 2010

Welcome 2010

Saturday, October 10, 2009

Directive Syntax @ Control

Directive Syntax @ Control

The @Control directive is used in an Asp.Net user controls. The @Control directive helps us to define control-specific attributes to be used by the ASP.NET control parser and compiler Hence it can be used only with .ascx (User Control) files.

The attributes of @Control directives are

a. Language: This Attribute is same as we read in the Page directive.

Value: Is a valid string which can represent any .NET-supported language, including VB || C# || Pearl || Jscript || any other .NET
Purpose: Tell the compiler about the language being used in the code-behind file.

b. AutoEventWireup: This Attribute is same as we read in the Page directive.
Value: Can be true || False. By Default value is true.
Purpose: For every page in a .net website there is an automatic way to bind the events to their corresponding methods in the same .ascx file and code behind.

c. CodeFile: This Attribute is same as we read in the Page directive.
Value: Any string which specifies the name of the code-behind file.
Purpose: Specifies the associated code-behind file of the page.


d. EnableViewState: This Attribute is same as we read in the Page directive.
Value: True || False The default is true.
Purpose: Tells whether view state is maintained across page requests. true to maintain view state is; otherwise, false.

e. Inherits: This Attribute is same as we read in the Page directive.
Value: any string
Purpose: Specifies a code-behind class name which the page will inherit. It can be any class which inherits the Page class.

f. Debug:
Value: Can be true || false
Purpose: This value indicates that the page should compile with debug symbols or without debug symbols.

g. Src:
Value: Any Valid String
Purpose: Tells the source file of the class i.e the file used as the code behind file of this user control.

The other attributes which we rarely use are ClassName, CompilerOptions, ComplieWith, Description, EnableTheming, Explicit, LinePragmas, Strict and WarningLevel.

An example of how a @Control directive looks


For more details Visit MSDN

Directive Syntax Page

Directive Syntax

Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control.

Directives can be located anywhere in an .aspx or .ascx file, but the standard practice is to include them at the beginning of the file (The advantage of using them at the top or the beginning of the file is that we need not to search them through out the file to make any minor change). Each directive can contain one or more attributes (paired with values, same as any HTML tag) that are specific to that directive.

Special Note:

The @ Page directive can be used only in .aspx files, and the @ Control directive can be used only in .ascx files.

The ASP.NET framework supports 11 types of directives and they are as follows.

@ Page: Can be included only in .aspx files. It defines page-specific attributes used by the ASP.NET page parser and compiler. Every .aspx files should include this @Page directive to execute. Throwing some light on important attributes of page directive

a. AspCompat:

Value: true || false.  By Default even if we don’t specify the value for AspCompat, it is set to false.
Purpose: Setting it to true will allow to the page to be executed on a single-threaded scenario.
For Example: The main cause when we set it to true is when we intend to use a component which was developed in VB 6.0; we can set this value to True.
Caution: Setting this attribute to true can cause page’s performance to degrade.

b. Language:

Value: Is a valid string which can represent any .NET-supported language, including VB, C#,  Pearl, Jscript any other .NET
Purpose: Tell the compiler about the language being used in the code-behind file.

c. AutoEventWireup:

Value: Can be true || False. By Default value is true.
Purpose: For every page in a .net website there is an automatic way to bind the events to their corresponding methods in the same .aspx file and code behind.

d. CodeFile:

Value: Any string which specifies the name of the code-behind file.
Purpose: Specifies the associated code-behind file of the page.

e. Title:

Value: Any String
Purpose: To set the page title, if it is different from what is specified in the master page.
f. Culture:

Value: string containing the culture value or auto.
Purpose: To specifies the culture setting of the page. If set to auto; it enables the page to automatically detect the culture required for the page.

g. ValidateRequest:

Value: True ||False The default is true.
Purpose: Indicates whether request validation should occur or not. In case of true, request validation checks all input data against a hard-coded list of potentially dangerous values. In case they match, an HttpRequestValidationException Class is thrown.
This feature is enabled in the machine configuration file (Machine.config). You can disable it in your application configuration file (Web.config) or on the page by setting this attribute to false.


h. Theme:

Value: any string
Purpose: To specify the theme for the page. This feature is introduced in Asp.Net 2.0.


i. SmartNavigation:

Value: True ||False The default value is false
Purpose: Specifies that the page should use smart navigation feature or not. Smart Navigation Feature enables a page to save the position after a postback. i.e. the page will returns the postback to current position of the page.

j. MasterPageFile:

Value: any String
Purpose: Specifies the location of the MasterPage file to be used with the current Asp.Net (.aspx) page.
k. EnableViewState:

Value: True ||False The default is true.
Purpose: Tells whether view state is maintained across page requests. true to maintain view state is; otherwise, false.

l. ErrorPage:

Value: Any string in URL format
Purpose: Specifies a target URL for redirection in case of an unhandled page exception occurs.
m. Inherits:

Value: any string
Purpose: Specifies a code-behind class name which the page will inherit. It can be any class which inherits the Page class.



There are also other attributes which are of seldom use such as Buffer, CodePage, ClassName, EnableSessionState, Debug, Description, EnableTheming, EnableViewStateMac, TraceMode, WarningLevel, etc.



An example of how a @Page directive looks



<Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Sample Page Title">


For More details Visit MSDN

Saturday, September 19, 2009

File Upload In silverlight using Web service (.asmx)

In My Article File Handling in Silverlight I have discussed that we can use file upload in tow ways one using the Isolated storage of the silerlight and the other is using the classic (.asmx) Web services or WCF. Here I am discussing about
the classic .asmx web services. To upload files in the silverlight applications.

Steps :

  1. Create a silverlight project in Visual Studio 2008 or beyond. As silverlight 3.0 is released so you can use that or go with Silverlight 2.0 hardly matters.
  2. Make these important changes in the properties of FileUploadASMX.Web project
    1. Right click on FileUploadASMX.Web.
    2. Select Properties
    3. Under Web tab Select Specific port and give the port number I am using port number 1366 Note: we are using specific port so that every time we need not to run the web service to use it and it wont change its address. Neither we ned it to host on IIS if you don't want to use specifc port then either host your web service on IIS or run the webservice every time and update the service refrence each time before running the application.
    4. Save The Properties Page and close it.
  3. Add a new Web service to the FileUploadASMX.Web project and name it as uploadService.asmx
  4. Include the name space "System.IO"
    using System.IO;
  5. Create a new web method//method for upload the files. This will be called from Silverlight code behind.
    [WebMethod]
    public string UploadFiles(string strFileName, byte[] byFile, string strFileNo)
    {
    try
    {
    if (byFile.Length > 0)
    {
    string strFilePath = "D:\\meet\\" + strFileName;
    System.IO.FileStream fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write);
    fs.Write(byFile, 0, byFile.Length);
    fs.Close();
    return strFileNo;
    }
    else
    {
    return "";
    }
    }
    catch
    {
    return "";
    }
    }
  6. Save the Service Leaving other settings as it is. or you want then you can delete the Helloworld example web method.
  7. Coming back to the FileUploadASMX project.
  8. Create A class FilesClasspublic class FilesClass
    {
    public FilesClass()
    {
    //do nothing
    }
    string strStatus = "";
    string strNo = "";
    FileInfo strFileName = null;
    public string PropNumber
    {
    get
    {
    return strNo;
    }
    set
    {
    strNo = value;
    }
    }
    public FileInfo PropFileName
    {
    get
    {
    return strFileName;
    }
    set
    {
    strFileName = value;
    }
    }
    public string PropStatus
    {
    get
    {
    return strStatus;
    }
    set
    {
    strStatus = value;
    }
    }
    }
  9. Create Two Buttons in the Mainpage.xaml.
    1. To Chosse File
    2. To upload File At the specifed folder in the Web Service
    The Code Will look like
    <Grid x:Name="LayoutRoot" Background="White" Width="400" Height="50">
    <Button Content="Choose" Height="20" Width="150" Click="Button_Click" Margin="10,0,10,10" VerticalAlignment="Bottom" d:LayoutOverrides="Width, Height" HorizontalAlignment="Left" Background="#FF2770AF"></Button>
    <Button Content="Upload" Height="20" Width="150" Click="Button_Click_1" Margin="20,0,10,10" VerticalAlignment="Bottom" d:LayoutOverrides="Width, Height" HorizontalAlignment="Right" Background="#FF2770AF"></Button>
    </Grid>

  10. Add the service refernce to the silverlight project name it "uploadService"
  11. Navigating to the event for choose buttonprivate void Button_Click(object sender, RoutedEventArgs e)
    {
    //create a oblect for file open dialog
    OpenFileDialog op = new OpenFileDialog();
    //show the file browser dialog to select file
    op.ShowDialog();
    //if any file is selected
    if (op.File != null && op.File.Name != "")
    {
    //check the file size. It shoud be less than 1MB
    if (op.File.Length < 1048576)
    {
    //create a new fill class object to maintain the file content
    FilesClass obj = new FilesClass();
    //assign file
    obj.PropFileName = op.File;
    //assing file number
    obj.PropNumber = iFileCount.ToString();
    //add the file object to files list collection
    fl.Add(obj);
    iFileCount++;
    }
    //IF size is greater then 1 MB show the message
    else
    {
    MessageBox.Show("Max file size is 1 MB");
    }
    }
    //display message to the user if no file is selected
    else
    {
    MessageBox.Show("Select File");
    }
    }
  12. Now Lets code for the upload event Navigate to the eventprivate void Button_Click_1(object sender, RoutedEventArgs e)
    {
    if (fl.Count > 0)
    {
    //loop through the files and upload the files by passing it to web service.
    for (int count = 0; count < fl.Count; count++)
    {
    uploadService.uploadServiceSoapClient x = new uploadService.uploadServiceSoapClient();
    //here is the event hadndler, whihc will be invoked for every file upload
    x.UploadFilesCompleted += new EventHandler<uploadService.UploadFilesCompletedEventArgs>(UploadFileComplted);
    //read the file from file class
    FilesClass obj = (FilesClass)fl[count];
    System.IO.FileStream str = obj.PropFileName.OpenRead();
    byte[] by = new byte[str.Length];
    str.Read(by, 0, by.Length);
    str.Close();
    //call the upload file methods async event, which will upload the files asynchronously
    x.UploadFilesAsync(obj.PropFileName.Name, by, obj.PropNumber);
    }
    }
    }
    private void UploadFileComplted(object sender, uploadService.UploadFilesCompletedEventArgs e)
    {
    if (e.Result != "")
    {
    MessageBox.Show("File Uploaded");
    }
    }
The Code Is Attached you can Download the files and enjoy file handling


Thanks and Regards
Meetu Choudhary
MVP.
My Blog || My Web || My Forums

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com