Mangal Pandey
Mangal Pandey

Reputation: 109

Not able to call a javascript function through alert

I am creating an alert and trying to call a click event through javascript function when "OK" of alert is pressed.It runs pretty well if I create the alert on page_Load but When I crate the alert on clicking of a buttton, then on pressing "OK" of alert the required click event is not called.

This is how I create the alert

protected void Button1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", "Test();", true);
    }

This is the javascript function which calls a click event

<script type="text/javascript">
          function Test() {
              alert('There is no Bookmarked Question Available');
              document.getElementById('btnReview').click();
          }
      </script>

Upvotes: 1

Views: 440

Answers (3)

Voonic
Voonic

Reputation: 4775

Try this

   protected void Button1_Click(object sender, EventArgs e)
    {
          string scripts = @"
          function Test() {

            alert('There is no Bookmarked Question Available');

          }";
          ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Startup", scripts, true);
 }

Upvotes: 0

Amul Harad
Amul Harad

Reputation: 146

You just add below code in javascript function

__doPostBack('btnSubmit','OnClick');

Upvotes: 0

Grundy
Grundy

Reputation: 13381

if btnReview is server button then try change you script like this

for asp.net

<script type="text/javascript">
      function Test() {
          alert('There is no Bookmarked Question Available');
          document.getElementById('<%= btnReview.ClientID %>').click();
      }
  </script>

for asp.net mvc (razor)

<script type="text/javascript">
      function Test() {
          alert('There is no Bookmarked Question Available');
          document.getElementById('@btnReview.ClientID').click();
      }
  </script>

Upvotes: 1

Related Questions