Typical applications:
Create a file: First, check whether the file on the server exists. If yes, a prompt box is displayed: Do you want to overwrite the file? If you confirm to overwrite, the new file will be created; otherwise, the system will directly return
Solution:
Add two buttons, one of which is hidden:
<Input type = button runat = server id = btnchk value = "Create File" onserverclick = "btnchk_serverclick"/>
<Input type = button runat = server id = btncrtfil onserverclick = "btncrt_serverclick" style = 'visibility: Den den '/>
Main btnchk_serverclick eventsCodeAs follows:
Crtfilchk (filename, ' Btncrtfil ' , Page );
The main code of the btncrt_serverclick event is as follows:
Createfile (filename, page );
First, determine if _ dopostback is used to call the code of the control on the server.
Public Void Crtfilchk ( String Strfilename, String Hidcontrolid, page srcpage)
{
Try
{
String Strtmp = "" ;
// Delete old files
If (File. exists (strfilename ))
{
Strtmp = Strfilename. Replace ( " \\ " , " / " );
Srcpage. registerstartupscript (system. guid. newguid (). tostring (),
" <SCRIPT> If ('overwrite? ') {__ Dopostback (' " + Hidcontrolid + " ', '') ;}</SCRIPT> " );
}
Else
{
Createfil (strfilename, srcpage );
}
}
Catch (Exception E)
{
Httpcontext. Current. response. Write ("<SCRIPT defer> alert ('Operation failed because"+E. Message+"') </SCRIPT>");
}
}
Create a file
Public Void Createfile ( String Strfilename)
{
Strfilename = Strfilename. Replace ( " \\ " , " / " );
Try
{
// Delete old files
If (File. exists (strfilename ))
{
File. Delete (strfilename );
}
String Strtmp = " File Content. " ;
Streamwriter SW = New Streamwriter (strfilename, False , System. Text. encoding. getencoding ( " Gb2312 " ));
Sw. Write (strtmp );
Sw. Close ();
Httpcontext. Current. response. Write ( " <SCRIPT defer> alert ('create a file " + Strfilename + " Successful! ') </SCRIPT> " );
}
Catch (Exception E)
{
Httpcontext. Current. response. Write ( " <SCRIPT defer> alert ('create a file " + Strfilename + " Failed because " + E. Message + " ! ') </SCRIPT> " );
}
}
Conclusion: Use _ dopostback to call the code of the server control