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 rpage_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>

This is the click event which will be called through Test()

 protected void btnReview_Click(object sender, EventArgs e)
    {
         count = int.Parse((string)ViewState["S.NO"]);
        dt1 = (DataTable)ViewState["Question"];
        if (rbOption.SelectedValue != "")
        {
            string strUserOpt = rbOption.SelectedItem.Text;
            strUserOpt = strUserOpt.Substring(20);
            dt1.Rows[count - 1][9] = strUserOpt;
            dt1.Rows[count - 1][10] = rbOption.SelectedValue;
        }
        lblReview.Visible = true;
        tblQues.Visible = false;
        tblReview.Visible = true;
        btnBookMark.Text = "Bookmark";
        btnBookMark.Font.Bold = false;
        btnBookMark.BackColor = Color.Empty;
        lblQuestionNo.Visible = false;
        lblTopic.Visible = false;
        lblTestHead.Visible = false;
        DataTable dt = new DataTable();
        dt.Columns.Add("Question");
        dt.Columns.Add("Status");
        dt.Columns.Add("BookMarked");
        DataRow dr1;
        foreach (DataRow dr in dt1.Rows)
        {
            dr1 = dt.NewRow();
            dr1[0] = dr[0].ToString() ;
            if (dr[9].ToString() != "") { dr1[1] = "Attempted"; } else { dr1[1] = "Un-attempted"; }
            if (dr[11].ToString() != "") { dr1[2] = "Yes"; } else { dr1[2] = "No"; }
            dt.Rows.Add(dr1);
        }
        dt.AcceptChanges();
        ClsDataBind.DoGridViewBind(grdReview, dt, _errMsg);
        btnBookMark.Visible = false;
        btnNext.Visible = false;
        btnPrevious.Visible = false;
        btnReview.Visible = false;

    }

Upvotes: 0

Views: 73

Answers (1)

Animesh Anand
Animesh Anand

Reputation: 324

The main problem may be you are clicking Button1 after btnReview because under btnReview_Click this happens

btnReview.Visible = false;

This means you will not be able to use Button1_Click event unless

btnReview.Visible = true;

Upvotes: 1

Related Questions