Obtain the results returned by confirm based on the background code before executing the background code)

Source: Internet
Author: User

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

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.