views:

922

answers:

2

Hi All,

I am looking to make a web control where I can register client startup scripts inline with my aspx because I hate registering in the codebehind!

An example of what I have so far:

<Ben:StartupScript runat="server">

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

</Ben:StartupScript>

Currently, I am overriding the OnPreRender method of the control, rendering the contents of the control to a string and then manually registering that string as a startup script using ClientScript.RegisterStartupScript on the Page. I also override the Render method in order not to render the control. I should also note that I have the ParseChildren attribute set to false and the PersistChildren attribute set to true.

All seems to be working well, but my control allows other web controls inside it (such as Button). Apart from being unintuitive, this can result in runtime errors. I would love to be able to clear the controls but this is impossible given the <% ... %> code block.

So, is it possible to prevent developers from embedding child controls whilst still allowing codeblocks?

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

Thanks a bunch,

Ben

+1  A: 

it sounds like a good idea, but if you spend too much time fighting the inherited/default behaviors then it may be more trouble than it's worth

if this is a one-shot issue, a cheap-hack solution is to just directly embed your scripts in the header of a master page ;-)

on the other hand, allowing developers to embed their own scripts as children of your web control might be useful

Steven A. Lowe
A: 

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

ClientScript.RegisterScript is mostly for user/server controls,

Matt Briggs