Register Run Script Control

I am looking to create a web control where I can register client startup scripts inside my aspx because I hate registering in code.

An example of what I have so far:

<Ben: StartupScript runat = "server">

var form = document.getElementById ("<% = form1.ClientID%>");

</ Ben: StartupScript>

I am currently overriding the control's OnPreRender method, passing the content of the control to a string, and then manually registering that string as a script run using ClientScript.RegisterStartupScript on the page. I also override the Render method to not render the control. I should also note that the ParseChildren attribute is set to false and the PersistChildren attribute is set to true.

Everything seems to work well, but my control allows other web controls (such as Button) to be used. Apart from being unintuitive, this can lead to runtime errors. I would like to be able to clean up controls, but this is not possible with a <% ...%> code block.

So, is it possible to prevent developers from injecting child controls while still preserving code blocks?

Also, is this a good idea or is it fundamentally flawed? lol.

Thanks a bunch,

Ben

+1


source to share


2 answers


it sounds good, but if you spend too much time on the inherited default behavior then it might be more trouble than it's worth

If it's a one-shot problem, a cheap hack solution is to simply embed your scripts in the header of the homepage; -)



on the other hand, allowing developers to inject their own scripts as children of your web control can be helpful

+1


source


If you are using master pages, add another control to your section. This way you can easily add css / js to your headers in your child pages.



ClientScript.RegisterScript is mainly for user / server controls,

0


source







All Articles