Question

I have to show confirmation dialogue on particular condition.And then proceed according to YES or No clicked.I tried with the following.

In 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" />

in 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
    }

But hdn_empname shows value="" while debuging.

Can anyone help me doing this?

Thanks in advance.

Was it helpful?

Solution 2

Where is your break point? If reader2.HasRows returns true your javascript will be registered. But it set the value on client and you get the result after postback.

OTHER TIPS

Try it You need to ClientID

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

I found out your main problems

The hidden field values will assign after the if condition call.

Edit :

So, You need to call your logic's in javascript side using ajax

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

//some code to execute
    }

hdn_empname is server controls Id which is different from client sided id, to get client sided id you need to use ClientID

try this:

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

You dont need to compare

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

this will work:

if (confirm("Employee Introduced already.Continue?")) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top