locked
Custom Control with child attributes/properties RRS feed

  • Question

  • User-709217888 posted

       Hi

    I'm fairly new to ASP.Net and am trying to write my own Custom Control, of which I need some help....

    Basically, I need a control such as....

    <MyCS:MyOutput ID="MyOutput1" runat="server" Initial="true">

        <MyCS:IntitialTemplate>
      --- put some javascript here
        </MyCS:IntitialTemplate>

        <MyCS:ErrorTemplate>
      --- put some javascript here
        </MyCS:ErrorTemplate>

    </MyCS:MyOutput>

    In essence, I need some child attributes which define some javascript that is rendered in the custom control's Render() method depending on the outcome of an operation server-side.

    My question is whether this can be done and how would the control be coded server side? So far as the child attributes/properties go, I've tried [ParseChildren(true)] & [PersistChildren(false)] but they don't seem to be doing anything.

    Can anyone offer any advice on how to code this?

    Thanks

    Jon


     

    Friday, April 18, 2008 4:03 PM

Answers

  • User-1136466523 posted

    Hi,

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>

    I’m afraid I’m not sure what you want to achieve. You mentioned that you want to assign the javascript codes to a property of the custom server control, which would be rendered in the render method of custom server control. But actually, for using javascript in custom server control, there are two general ways for you to achieve that. One is to regard the javascript as the embedded resource.

    <o:p> </o:p>

    In this way, you should add all of your javascript files which will be used in your control into your project, set the build action as the embedded resource, and declare the meta data in assemblyinfo.cs. And use GetWebResourceUrl method to retrieve the resource in your custom server control.

    <o:p> </o:p>

    Another way is to get the javascript code from the properties which exposed by the custom server control, in this way, you can use Page.ClientScript.RegisterExpandoAttribute() method to add the js code to render on the control.

    <o:p> </o:p>

    Thanks.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 22, 2008 11:30 PM