Author: love.net www.ASPCool.com Time: 2002-1-15 17:55:58 Views: 6848 http://www.aspcool.com/lanmu/browse1.asp?ID=820&bbsuser=aspnet
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 back-end code is compiled 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 Successfully!"; return;} catch (System.Exception exc) {label.Text = exc.Message; return;}} label.Text = "Please select A File to Upload! "; Return;} private string savepath =" "; private string extension ="; private string fileLength = "0";