0

I am trying to get user confirmation from c# code behind file, to confirm if user wants to overwrite the existing file on server of cancel the operation but so far no luck.

this is my code :

if (File.Exists(filePath))
{
   string alertMsg = @"confirm('A File already exists with the same name. \n Would you like to overwrite the existing file ?');";
   ScriptManager.RegisterStartupScript(this, this.GetType(), "Test", alertMsg, true);
}
else
{
   fuPSD.SaveAs(filePath);
}

any help would be highly appreciated.

4

3 回答 3

0

像这样的东西:

创造

<asp:hiddenField id="hf" />* in the aspx file
also put on the JS location a Declaration : 

var answer=null;

返回服务器:

string alertMsg = @"if (confirm('A File already exists with the same name. \n Would you like to overwrite the existing file ?')==true) answer='1'; else answer='0';";

现在在服务器端:阅读:

hf.value
于 2012-04-07T13:33:12.030 回答
0

如果文件存在,该代码将弹出消息,如果不存在则保存。

但是您需要对客户端的确认对话结果采取一些措施,并在覆盖的情况下触发回发或其他一些服务器调用以执行操作。

于 2012-04-07T13:34:58.427 回答
0

问题是您的 JavaScript 注册将要求用户 在下一页加载后确认覆盖该文件。然后,这提出了一个问题,即告诉用户是从服务器确认还是拒绝,因为您是在客户端确认。

过去,我使用隐藏字段来切换真/假以显示来自服务器端的弹出窗口。这使我能够将事件连接到代码后面的委托(即用我自己的按钮制作我自己的确认框)。你仍然可以在没有它的情况下执行此操作,因为我发现它会导致很多混乱且难以遵循脚本,例如手动调用 __doPostback。

于 2012-04-07T13:38:44.307 回答