JavaScript: Alert.Show(Message) from ASP.NET Code-Behind

JavaScript: Alert.Show(message) From ASP.NET Code-behind

Here is an easy way:

Response.Write("<script>alert('Hello');</script>");

How to call Javascript alert from code behind in asp.net

"I want to show the first alert message before LoadGridView() method".

You can't. You appear not to understand your page's lifecycle.

Basically when your browser tries to load your aspx page, it sends a HTTP request to the server. ASP.NET runs the code in your aspx code-behind. Then it returns the resulting data (i.e. a web page, which may include HTML, CSS and JavaScript) to the browser. Then the server stops executing the code. The page is now in the user's browser. At that point, any JavaScript code included in the page will be compiled and executed by the browser. No more .NET code runs again until the browser makes another request to the server (e.g by clicking on a link or submitting a form or something).

In summary - JavaScript and .NET code run at different times, in different contexts. You can't make them interlock like that using a conventional lifecycle.

If you wanted to display a message on screen while your gridview is loading, then you'd have to load the page, display the message, and the trigger the loading of the gridview via an AJAX request (in ASP.NET WebForms you can often implement this nicely by using an UpdatePanel control), so that the loading happens in a separate request to the server (and an AJAX request specifically doesn't cause the rest of your page to re-load in the meantime).

ASP.NET : Displaying an alert from C# code-behind

Description

Assuming i understand your question.

You can use the ScriptManager to show a javascript alert message.

Sample

protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"err_msg",
"alert('Dispatch assignment saved, but you forgot to click Confirm or Cancel!)');",
true);
}

More Information

  • MSDN - ClientScriptManager.RegisterStartupScript Method

How to display an alert box from C# in ASP.NET?

After insertion code,

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

How to put an alert box using javascript and asp.net?

You can use ScriptManager.RegisterStartupScript in code behind to show alert message like below . You need to Scriptmanager control to your aspx page . This is the recommended way

 ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), 
"err_msg",
"alert('Upload failed!');",
true);

Otherwise you can simple do like below

Response.Write("<script>alert('Upload failed!');</script>");

Link for scriptmanager control

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx

C# web application javascript alert in code behind

I would suggest that you first try to figure out if the code you expect is being returned to the page.

There are few ways to do this, with the easiest being "View Source" on the page after your above code runs. If you can find that content in the page, you will know that the content is being returned to the page but not executed.

If you do not see the content when viewing source, then you have a different problem. Either control is being passed to another page, or its not actually going to the if condition you expect.

Once you understand where the problem originates from these two pieces, you can start to find what may be causing it.

It is also somewhat apparent that you may not completely understand how the web works. Given this set of code....

       ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "return confirm('Are you sure you want to delete');", true);
if(true)
{
DataSet5TableAdapters.sp_inleaverequestTableAdapter TA = new DataSet5TableAdapters.sp_inleaverequestTableAdapter();
TA.GetData(lbl_empcode.Text, lbl_empname.Text, lbl_dept.Text, txtfromdate.Text, txttodate.Text, Convert.ToString(noofday), ddlleavetype.SelectedItem.Text, Convert.ToString(totalday), txtreason.Text, ddlforward.SelectedItem.Text, " ", " ", " ", " ", lbl_date.Text);

Send_Email(lbl_empcode.Text, lbl_empname.Text, txtfromdate.Text, txttodate.Text, noofday, ddlleavetype.SelectedItem.Text, txtreason.Text);
clear();
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Request sent successfully. Go and watch your status..');</script>", false);
}

... it appears that you are expecting the page execution on the server to stop and then wait for the client to respond. This is not how the web page life cycle works. The server code is going to execute completely and send content back to the client. Its not an interactive process (even with AJAX) like a windows forms or console application.

How to show alert from code behind after page loads completely?

First of all, you should be using the following command according to the MSDN website (http://msdn.microsoft.com/en-us/library/asz8zsxy(v=vs.110).aspx):

Note: the script should be within <script></script>.

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>alert('Connection Successful!');</script>", true);

The script is added to the top of your page, before the content, so the alert is displayed before your page is rendered. In order to fix this issue, use:

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>window.onload = function(){alert('Connection Successful!');}</script>", true);

By doing so, you ensure that only when the page (JavaScript window object) loads the alert is displayed.

If you are using jquery, you can also use:

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Message", "<script type=text/javascript>$(window).load(function() {alert('Connection Successful!');});</script>", true);

Code Behind JavaScript alert with redirecting

You can try the following:

ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect",
"alert('test 9'); window.location='" +
Request.ApplicationPath + "/anotherpage.aspx';",true);


Related Topics



Leave a reply



Submit