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