关于根据后台代码获取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事件主要代码如下:

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

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

None.gif public   void  CrtFilChk( string  strFileName, string  HidControlID,Page srcPage)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
string strTmp ="";
InBlock.gif            
//删除旧文件
InBlock.gif
            if (File.Exists(strFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            CreateFil(strFileName,srcPage);
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif         }

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

ExpandedBlockEnd.gif    }
  
None.gif

创建文件

None.gif public   void  CreateFile( string  strFileName)
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {       
InBlock.gif strFileName
= strFileName.Replace("\\""/");  
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
//删除旧文件
InBlock.gif
            if (File.Exists(strFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif        
dot.gif{           
InBlock.gif            HttpContext.Current.Response.Write(
"<script defer>alert('创建文件" + strFileName + "失败,因为"+e.Message+"!')</script>");
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif }

None.gif

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值