Monday, August 18, 2014

Filled Under:
, ,

Display alert message in asp.net code behind

Display alert message in asp.net code behind

In this topic we are going to see how to Display alert message in asp.net code behind using C# and Vb.net

Using Page.ClientScript.RegisterClientScriptBlock:-

The RegisterClientScriptBlock method allows you to place a JavaScript function at the top of the page. This means that the script is in place for the startup of the page in the browser.



C#
vb
output

From this example, you can see that you create the JavaScript function AlertHello() as a string called myScript. Then using the Page.ClientScript.RegisterClientScriptBlock method, you program the script to be placed on the page.

Using Page.ClientScript.RegisterStartupScript

The RegisterStartupScript method is not that different from the RegisterClientScriptBlock method. The big difference is that the RegisterStartupScript places the script at the bottom of the ASP.NET page instead of at the top.



C#
VB
  Output

So what difference does it make where the script is registered on the page?
A lot, actually!
If you have a bit of JavaScript that is working with one of the controls on your page, in most cases you want to use the RegisterStartupScript method instead of RegisterClientScriptBlock.

For example:- you use the following code to create a page that includes a simple <asp:TextBox> control that contains a default value of “Hello” Then use theRegisterClientScriptBlock method to place a script on the page that utilizes the value in the TextBox1 control.






0 comments:

Post a Comment