Everyone knows Microsoft in ASP.NET to provide us with a lot of server-side controls, including HTMLControl and WebControl. They are powerful and provide great convenience for our programming. More importantly, it has opened the use of third-party controls. This allows us to customize the server-side controls they need.
Here's how to customize a server-side control with an integrated upload component, which is actually a collection of HTMLINPUTFILE, Button and Label, and event implementation. The function to be achieved by this component is to automatically implement files with a markup that sets a few properties, without having to implement an event, etc.
Write backend code to compile into a DLL
// File Name: WMJWEBControls.cs
Using system.drawing;
Using system.Web.ui.htmlcontrols;
Using system.Web.ui.webcontrols;
Using system;
Namespace WMJ
{
Public Class Fileupload: Panel
{
Private HTMLINPUTFILE HTMLINPUTFILE;
Private button button;
PRIVATE LABEL LABEL;
Public fileupload (): Base ()
{
HTMLINPUTFILE = New HTMLINPUTFILE ();
Button = new button ();
Button.text = "Upload";
Button.click = new eventhandler (this.button_click);
Label = new label ();
Label.Text = " Select the path of the upload file font>";
This.Controls.add (htmlinputfile);
This.Controls.Add (Button);
This.Controls.Add (label);
THIS.WIDTH = 450;
THIS.HEIGHT = 30;
This.borderstyle = BorderStyle.dotted;
THIS.BORDERWIDTH = 1;
}
Private Void Button_Click (Object Sender, Eventargs E)
{
System.Web.httppostedFile PostedFile = HTMLINPUTFILE.POSTEDFILE
IF (PostedFile! = NULL)
{
Try {
String filename = pathtoname (postedfile.filename);
IF (! filename.endswith (extension))
{Label.Text = "You Must SELECT" EXTENSION "File!"; Return;}
IF (PostedFile.ContentLength> INT.PARSE (FileLength))
{Label.Text = "File Too Big!"; Return;}
PostedFile.saveas (SavePath FileName);
Label.Text = "Upload File Success"; "
Return;
} catch (system.exception exc) {label.text = exc.Message; Return;
}
Label.Text = "Please select a file to upload!";
}
Private string saveparth = "";
PRIVATE STRING EXTENSION = ""
Private string fileLength = "0";
// Uploaded files Save the location of the server defaults to C: / These properties are generally settings in the ASP.NET tags can also be set in CodeBehind.
Public String SavePath
{
get
{
IF (SavePath! = "") Return SavePath;
Return "C: //";
}
set
{
SavePath = Value;
}
}
// Upload the maximum length unit K default is 1K
Public String FileLength
{
get
{
IF (FileLength! = "0") Return FileLength;
Return "1024";
}
set
{
FileLength = (int.Parse (Value) * 1024) .tostring ();
}
}
// Upload the extension of the file default to TXT
Public String Extension
{
get
{
IF (extension! = ") RETURN EXTENSION;
Return "TXT";
}
set
{
EXTENSION = VALUE;
}
}
Public String Pathtoname (String Path)
{
INT POS = path.lastIndexof ("//");
Return path.substring (POS 1);
}
}
}
Compile this file into DL, L in the bin directory you want to use, you can pass it in the website.
Use this component
Let's take an example of calling this control
<% @ Page language = "c #"%>
<% @ Register tagprefix = "wmj" namespace = "wmj" assembly = "wmjwebControls"%>
hEAD>