[Learning] Create server controls

xiaoxiao2021-03-06  21

// 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 ";

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!";

Return;

}

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);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-44383.html

New Post(0)