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?
RegisterClientScriptBlockdoes 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*.jsfile. 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 ajavascriptmethod to it like that:For this to work you must have
javascript_method()defined in a*.jsfile or added to your page viaRegisterClientScriptBlock