Web control development-perfect upload and download controls "new" (iii)

Source: Internet
Author: User

After the introduction of the public properties of the control, the next step is to process the control event. event processing is divided into server events and client events. For server-side events, in addition to the internal deletion and upload button clicking event, the component also exposes an onafteroperation event, which is triggered when the upload is successful or the deletion is successful, based on the command name passed to the event, you can determine whether the event is deleted or uploaded successfully. the reason for setting this event is to prepare for filepicker development in the future. The interface is as follows:


Click the image button as follows:


When the upload and download components in the pop-up window are successfully uploaded, the preview window should be refreshed. Therefore, this server event is set.

The following are specific server events:Code

Server event # Region Server event

// Delete button event processing

Private   Void Delete_clicked (Object sender, system. Web. UI. imageclickeventargs E)

{

If ( This . Filename ! = String . Empty)

{

System. Io. file. Delete ( This . Page. mappath ( This . Fullfilename ));

This . MessageBox ( " Deleted successfully! " );

This . Filename =   String . Empty;

// Trigger event upon success

Onafteroperation ( New Commandeventargs ( " Delete " , Null ));

}



}

// Dialog Box displayed when the operation is successful



Private   Void MessageBox ( String Strinfo)

{

Strinfo = " <SCRIPT> alert (' " + Strinfo + " ') </SCRIPT> " ;

If ( This . Page ! =   Null )

This . Page. registerstartupscript (system. guid. newguid (). tostring (), strinfo );

}

// Convert all extensions to lowercase characters during initialization for future verification

Protected   Override   Void Oninit (eventargs E)

{

Base . Oninit (E );

Foreach (Stringitem item In   This . Extfilters)

{

Item. Text=Item. Text. tolower ();

}

}


// Upload button event

Private   Void Upload_clicked (Object sender, system. Web. UI. imageclickeventargs E)

{

System. Web. httppostedfile File =   This . Fileupload. postedfile;

String Extname = File. filename. substring (file. filename. lastindexof ( " . " ) +   1 );



If (File. contentlength >   0 )

{

// Verify the Server File Type

Stringitem item =   New Stringitem ();

Item. Text = Extname. tolower ();

If (( This . Extfilters. Count >   0 ) && ( ! This . Extfilters. Contains (item )))

{

This. MessageBox ("The file type cannot be uploaded!");

Return;

}

Switch ( This . Uploadfilenametype)

{

Case Filenametype. autogenerate:

// The GUID is used to automatically generate a file name.

This . Filename = System. guid. newguid (). tostring () +   " . "   + Extname;

Break ;

Case Filenametype. clientside:

// Get client file name

This . Filename = File. filename. substring (file. filename. lastindexof ( " \\ " ) + 1 );

Break ;

}

If ( This . Filename ! = String . Empty)

{

File. saveas ( This . Page. mappath ( This . Fullfilename ));

This . MessageBox ( " Upload successful! " );

// Trigger event upon success

Onafteroperation ( New Commandeventargs ( " Upload " , Null ));

}

}

}

// Event key

Private   Static   Readonly   Object Eventop =   New   Object ();

Public   Event Commandeventhandler afteroperation

{< br>
Add {events. addhandler (eventop, value) ;}
remove {events. removehandler (eventop, value) ;}
}

Protected   Virtual   Void Onafteroperation (commandeventargs E)

{

Commandeventhandler ophandler = (Commandeventhandler) events [eventop];

If (Ophandler ! =   Null )

{

Ophandler (This, E );

}

}

# Endregion

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.