Click here to Skip to main content
15,918,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to show alert message on the page where I click on the button not on the blank page

please help

What I have tried:

C#
string alertMessage = "<script language=\"javascript\" type=\"text/javascript\">";

alertMessage += "alert('No quiz avalible');";
alertMessage += "window.location.href=\"";
alertMessage += "View.aspx?Id=" + Request.QueryString["Id"].ToString();
alertMessage += "\";";
alertMessage += "</script>";

ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage ", alertMessage)
Posted
Updated 3-Aug-16 5:04am
v2
Comments
Karthik_Mahalingam 3-Aug-16 9:47am    
what is the error?
you code works fine.
Member 12618369 3-Aug-16 9:59am    
The error is alter message is displayed on blank page
Richard Deeming 3-Aug-16 10:02am    
Then add some content to the page!
Karthik_Mahalingam 3-Aug-16 10:04am    
check my solution.

1 solution

The Javascript code block executes before the DOM[^](page) is loaded, thats why the page is blank.
Just add jquery[^] reference to the page and modify the code as below. it should work.

C#
protected void Button1_Click(object sender, EventArgs e)
      {

          string alertMessage = "<script language=\"javascript\" type=\"text/javascript\">";
          alertMessage += "   $(function () { ";
          alertMessage += "alert('No quiz avalible');";
          alertMessage += "window.location.href=\"";
          alertMessage += "View.aspx?Id=" + Request.QueryString["Id"];
          alertMessage += "\"; });";
          alertMessage += "</script>";
          ClientScript.RegisterClientScriptBlock(GetType(), "alertMessage ", alertMessage);
      }


jquery reference
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


refer this: jquery dom ready event[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900