C# asp.net web表单应用程序中的确认框

C# asp.net web表单应用程序中的确认框,c#,asp.net,webforms,C#,Asp.net,Webforms,我想给用户警告确认消息框,如果他们想保存而不上传图像 当用户单击“保存”按钮时激发代码 if (string.IsNullOrEmpty(obj.Image)) { obj.Image= null; //Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('Please Upload Image!');", true); Page.ClientScript.Register

我想给用户警告确认消息框,如果他们想保存而不上传图像

当用户单击“保存”按钮时激发代码

if (string.IsNullOrEmpty(obj.Image)) {
    obj.Image= null;
    //Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('Please Upload  Image!');", true);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "confirm('Are you sure you want to save without uploading  images?');", true);
}
上述逻辑用于显示确认框,但未显示

除此之外,如果用户单击了
Yes
No
按钮,我该如何设置陷阱

这样我就可以相应地触发相关代码

更新:

我可以用以下代码显示确认框

Page.ClientScript.RegisterStartupScript(this.GetType(), "msgbxConfirm", "confirm('Are you sure?');", true);

但我不确定如何捕获
YES
No
事件来处理确认弹出框的结果-检查返回值

如果单击“确定”,它将返回true,否则将返回false

例如:

编辑:评论

要在用户单击确认对话框上的“确定”时重新提交表单,您需要这样做:

代码隐藏:

Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "msgbxConfirm", 
    "doConfirm();", 
    true
);
<script type=text/javascript>

function doConfirm() {
    if(confirm('Are you sure?') {
        document.getElementById(<id_of_form_submit_button>).click();
    }
}
</script>
aspx页面:

Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "msgbxConfirm", 
    "doConfirm();", 
    true
);
<script type=text/javascript>

function doConfirm() {
    if(confirm('Are you sure?') {
        document.getElementById(<id_of_form_submit_button>).click();
    }
}
</script>

函数doConfirm(){
如果(确认('你确定吗?')){
document.getElementById()。单击();
}
}

要处理确认弹出框的结果,请检查返回值

如果单击“确定”,它将返回true,否则将返回false

例如:

编辑:评论

要在用户单击确认对话框上的“确定”时重新提交表单,您需要这样做:

代码隐藏:

Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "msgbxConfirm", 
    "doConfirm();", 
    true
);
<script type=text/javascript>

function doConfirm() {
    if(confirm('Are you sure?') {
        document.getElementById(<id_of_form_submit_button>).click();
    }
}
</script>
aspx页面:

Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "msgbxConfirm", 
    "doConfirm();", 
    true
);
<script type=text/javascript>

function doConfirm() {
    if(confirm('Are you sure?') {
        document.getElementById(<id_of_form_submit_button>).click();
    }
}
</script>

函数doConfirm(){
如果(确认('你确定吗?')){
document.getElementById()。单击();
}
}

我在这里添加的一点是,您无法在服务器端代码中获得确认结果,您必须在JS端编写该代码。因此,为了更好地维护它,您可以在aspx中使用以下JS

function ConfirmAndExecute()
{
    if(confirm("Are you sure?"))
    {
        OnConfirm();
    }
    else
        alert("you missed !")
}
function OnConfirm()
{
    // JS Code to handle post confirm operation
}
在服务器端,页面加载,使用以下命令


Page.ClientScript.RegisterStartupScript(this.GetType(),“msgbxConfirm”,“ConfirmAndExecute();”,true);

我在这里添加的一点是,您无法在服务器端代码中获得确认结果,您必须在JS端编写该代码。因此,为了更好地维护它,您可以在aspx中使用以下JS

function ConfirmAndExecute()
{
    if(confirm("Are you sure?"))
    {
        OnConfirm();
    }
    else
        alert("you missed !")
}
function OnConfirm()
{
    // JS Code to handle post confirm operation
}
在服务器端,页面加载,使用以下命令


Page.ClientScript.RegisterStartupScript(this.GetType(),“msgbxConfirm”,“ConfirmAndExecute();”,true);

您也可以只添加一个onclick客户端脚本事件句柄,以防止表单提交,除非确认为true,这样您就不必处理脚本注册。只需将下面的代码添加到
asp:button
定义中即可

<asp:Button id="btnSave" Text="Save" runat="server" OnClientClick="return confirm('Are you sure?');" /> 

您也可以只添加onclick客户端脚本事件句柄,以防止表单提交,除非确认为true,这样您就不必处理脚本注册。只需将下面的代码添加到
asp:button
定义中即可

<asp:Button id="btnSave" Text="Save" runat="server" OnClientClick="return confirm('Are you sure?');" /> 


您不在函数中,因此您不需要
return
语句-这可能导致语法错误,这就是为什么方框不显示我之前删除了该框的原因让我更新问题代码。您不在函数中,因此您不需要
return
语句-这可能导致语法错误,这就是框未显示我已删除了之前的“让我更新问题代码”。这是使用java脚本,但如何从文件c后面的代码中执行?您要实现什么?看起来您正在验证表单条目(文件上载)使用codebehind可能更适合javascript检查。这是使用java脚本,但如何从代码隐藏文件c中执行它?您要实现什么?看起来您正在验证表单条目(文件上载)使用codebehind可能更适合javascript检查。这很好,但我需要检查codebehind中的状态。我认为可以通过在隐藏字段中设置值并在codebehind中访问此隐藏字段的值来完成……是的,你是对的!@KnowledgeSeeker,我想告诉你
什么不能做ne
,这就是我提到它的原因。这很好,但我需要检查代码隐藏中的状态。我认为可以通过在隐藏字段中设置值并在代码隐藏中访问此隐藏字段的值来完成……是的,你是对的!@KnowledgeSeeker,我想告诉你什么不能做,这就是我提到它的原因。Ni简单,谢谢。除了我必须写:ie…把OnClick改成OnClientClick和简单。谢谢。除了我必须写:ie…把OnClick改成OnClientClick