关于根据后台代码获取confirm返回结果后再执行后台代码(转)

典型应用:
新建文件:首先判断服务器上文件是否存在,如果存在则出现提示框:是否需要覆盖?如果确认覆盖则继续新建文件,否则直接返回
解决方法:
增加两个按钮,其中一个隐藏:
<input type=button runat=server id=btnChk value="创建文件" onserverclick="btnChk_ServerClick"/>
<input type=button runat=server id=btnCrtFil onserverclick="btnCrt_ServerClick" style='visibility:hidden' />

btnChk_ServerClick事件主要代码如下:

CrtFilChk(filename,'btnCrtFil',Page);
btnCrt_ServerClick事件主要代码如下:
CreateFile(filename,Page);

先判断如在则采用__doPostBack调用服务器端控件的代码

public void CrtFilChk(string strFileName,string HidControlID,Page srcPage)
    
{
        
try
        
{
        
string strTmp ="";
            
//删除旧文件
            if (File.Exists(strFileName))
            
{
            strTmp
= strFileName.Replace("\\""/");            
            srcPage.RegisterStartupScript(System.Guid.NewGuid().ToString(),
            
"<script> if(('覆盖吗?')){__doPostBack('"+HidControlID+"','');}</script>"); 
            }
    
            
else
            
{
            CreateFil(strFileName,srcPage);
            }
        
         }

        
catch (Exception e)
        
{         
            HttpContext.Current.Response.Write(
"<script defer>alert('操作失败,因为"+e.Message+"')</script>");
        }

    }
  

创建文件

public void CreateFile(string strFileName)
 
{       
 strFileName
= strFileName.Replace("\\""/");  
        
try
        
{
        
//删除旧文件
            if (File.Exists(strFileName))
            
{            
                File.Delete(strFileName);
            }
       
            
        
string strTmp ="文件内容."
            StreamWriter sw 
= new StreamWriter(strFileName, false, System.Text.Encoding.GetEncoding("gb2312"));
            sw.Write(strTmp);
            sw.Close();
           
            HttpContext.Current.Response.Write(
"<script defer>alert('创建文件" + strFileName + "成功!')</script>");
        }
 
        
catch (Exception e)
        
{           
            HttpContext.Current.Response.Write(
"<script defer>alert('创建文件" + strFileName + "失败,因为"+e.Message+"!')</script>");
        }
        
 }

小结:采用__doPostBack调用服务器控件的代码

posted @ 2007-04-28 14:09  liudao  阅读(3437)  评论(0编辑  收藏  举报
51Aspx.com 版权所有