views:

56

answers:

2

This is some Javascript added on an .aspx page:

<Head>
<script>
function SayHello()
{
    alert('Hello');
}
</script>

and here is how I am calling it from code:

protected void Page_Init(object sender, Eventargs e)
{
    Page.ClientScript.RegisterStartupScript(Page.GetType(),"script","SayHello()",true); 
}

The problem is that the Javascript function is called only one time, on the very first page load. I want it to be called on every post back. for example on every button click

A: 

Create a subclass of the Button control and use that on your pages. It has the OnClientClick property which you can already preset in your subclass.

XIII
A: 

If you want to stick with an ASP.NET solution, then put your code into the Page_Load event, rather than the Page_Init event. Page_Init isn't triggered on postbacks.

However, I would recommend a pure JavaScript method, such as adding the call to the Javascript function in the body onload event:

<body onload="sayHello();">

Or using the jQuery library to hook onto the document's onready event:

$(document).ready(function() { sayHello(); });
attack