Javascript Confirmation On if..else.. condition



我必须在特定条件下显示确认对话框。然后根据"是"或"否"单击继续。我尝试了以下内容。

在 aspx 中:

<script type="text/javascript">
  function ShowConfirmation() {
    if (confirm("Employee Introduced already.Continue?") == true) {
      document.getElementById("hdn_empname").value = 1;
    }
  }
</script>
<asp:HiddenField ID="hdn_empname" runat="server" />

在 CS 中:

  if (reader2.HasRows)
    {  
        Page.ClientScript.RegisterStartupScript(this.GetType(), "showAl", "ShowConfirmation();", true);
    }
    else
    {
        hdn_empname.Value ="1";
    }
    if ((hdn_empname.Value)=="1")
    {
       //some code to execute
    }

hdn_empname在调试时显示value=""

谁能帮我做这件事?

提前谢谢。

试试看 你需要ClientID

document.getElementById('<%=hdn_empname.ClientID%>').value = 1;

我发现了你的主要问题

隐藏字段值将在 if 条件调用后分配。

编辑:

所以,你需要使用 ajax 在 javascript 端调用你的逻辑

if (confirm("Employee Introduced already.Continue?") == true) {
//some code to execute
    }

你的断点在哪里?如果读者2。HasRows返回true,你的javascript将被注册。但它在客户端上设置了值,并在回发后获得结果。

hdn_empname是服务器控件 Id,它与客户端 ID 不同,要获取客户端 ID,您需要使用 ClientID

试试这个:

document.getElementById('<%=hdn_empname.ClientID%>').value = "1";

你不需要比较

if (confirm("Employee Introduced already.Continue?") == true)

这将起作用:

if (confirm("Employee Introduced already.Continue?")) 

最新更新