How to customize a server-side control

Source: Internet
Author: User
Tags file upload
Server | Control everyone knows that in ASP.net, Microsoft provides us with a large number of server-side controls, including HTMLControl and WebControl. They are powerful and provide great convenience for our programming. More importantly, it opens up the use of Third-party controls. This allows us to customize the server-side controls we need.
Below I will use an integrated upload component to illustrate how to customize a server-side control, which is actually a collection of HtmlInputFile, Button and label, and event implementations. This component is to achieve the same function as the existing WebControl, with a set of several attributes of the tag can automatically implement file upload, without the implementation of the event and so on.
Write back-end 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= "<font size=2> Please choose the path of uploading 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";
The uploaded file is saved on the server by default C:\ These properties are typically set in the ASP.net tag or set in Codebehind
public string Savepath
{
Get
{
if (savepath!= "") return Savepath;
return "c:\\";
}
Set
{
Savepath=value;
}
}
The maximum length unit K for the uploaded file is 1k by default
public string Filelength
{
Get
{
if (filelength!= "0") return filelength;
return "1024";
}
Set
{
filelength= (int. Parse (value) *1024). ToString ();
}
}
The upload file extension defaults 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 and place it in the bin directory where you want to use it.
<wmj:fileupload savepath= "e:\\" filelength= "3" extension= "TXT" runat= "server"/>
Using this component.
Here's an example of calling this control
<% @page language= "C #"%>
<!--Note that the following sentence is necessary-->
<%@ Register tagprefix= "WMJ" namespace= "WMJ" assembly= "Wmjwebcontrols"%>
<body>
<form enctype= "Multipart/form-data" runat= "Server" >
<wmj:fileupload savepath= "e:\\" filelength= "3" extension= "TXT" runat= "server"/>
How to use the <!--it's so simple, a little bit permanent.-->
</form>
</body>
With this example, you don't have to worry about ASP.net server controls too little.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.