Question

I'm trying to register a simple script -

ClientScript.RegisterClientScriptBlock(typeof(Page), "myscript", "<script>testfun();</script>");

This line works fine when I put it in the page load event. However, everytime the user selects a row in the gridview, I need some client side function to get executed, hence in the GridView.SelectedIndexChanged event, I added the same line, however, it doesn't work there. Can anyone point out why this happens or direct me towards a more elegant solution?

Était-ce utile?

La solution

RegisterClientScriptBlock does not execute your script but only emits it into the page that is then sent to a client. It is a programmatic equivalent of having a *.js file. The script is only executed when the resulting HTML page is loaded on the client. If you want some java script to run on particular control's event you need to hook up a javascript method to it like that:

onclick="javascript_method();"

For this to work you must have javascript_method() defined in a *.js file or added to your page via RegisterClientScriptBlock

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top