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

本文介绍在ASP.NET中如何实现文件的创建与覆盖功能,通过使用两个按钮来完成文件是否存在时的提示与选择,提供了具体的代码实现,并讨论了在文件已存在时如何进行覆盖操作。

典型应用:
新建文件:首先判断服务器上文件是否存在,如果存在则出现提示框:是否需要覆盖?如果确认覆盖则继续新建文件,否则直接返回
解决方法:
增加两个按钮,其中一个隐藏:
<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事件主要代码如下:

None.gifCrtFilChk(filename,'btnCrtFil',Page);
None.gifbtnCrt_ServerClick事件主要代码如下:
None.gifCreateFile(filename,Page);

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

None.gifpublic void CrtFilChk(string strFileName,string HidControlID,Page srcPage)
ExpandedBlockStart.gif    {
InBlock.gif        try
ExpandedSubBlockStart.gif        {
InBlock.gif        string strTmp ="";
InBlock.gif            //删除旧文件
InBlock.gif
            if (File.Exists(strFileName))
ExpandedSubBlockStart.gif            {
InBlock.gif            strTmp= strFileName.Replace("\\", "/");            
InBlock.gif            srcPage.RegisterStartupScript(System.Guid.NewGuid().ToString(),
InBlock.gif            "<script> if(('覆盖吗?')){__doPostBack('"+HidControlID+"','');}</script>"); 
ExpandedSubBlockEnd.gif            }    
InBlock.gif            else
ExpandedSubBlockStart.gif            {
InBlock.gif            CreateFil(strFileName,srcPage);
ExpandedSubBlockEnd.gif            }        
ExpandedSubBlockEnd.gif         }
InBlock.gif        catch (Exception e)
ExpandedSubBlockStart.gif        {         
InBlock.gif            HttpContext.Current.Response.Write("<script defer>alert('操作失败,因为"+e.Message+"')</script>");
ExpandedSubBlockEnd.gif        }
ExpandedBlockEnd.gif    }  
None.gif

创建文件

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


本文转自 liudao 博客园博客,原文链接:xxhttp://www.cnblogs.com/liudao/archive/2007/04/28/731034.html如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值