AZGroups.com

Bringing together Arizona Technology Users And Enhancing the Careers of Developers
Welcome to AZGroups.com Sign in | Join | Help
in Search

asp.net custom control

Last post 03-21-2007, 3:09 PM by RabidSniper. 10 replies.
Sort Posts: Previous Next
  •  04-05-2006, 5:30 PM 125

    asp.net custom control

    I'm not sure if I should post this to the mailing list as well, or what (transition is hard!).

    I want to be able to create a custom server control that will let me do something like this:

    <foo:myCustomControl id="foo1" runat="server">

       <asp:label id="lblTest" runat="server" /><div>hello</div><asp:button id="btnSubmit" runat="server" text="Submit" />

    </foo:myCustomControl>

    In essence, a custom control that acts exactly like any other asp.net control does (imagine replacing foo:myCustomControl w/ asp:Panel), and will appropriately render child controls. I know there's difficulties associated with doing this...has anyone managed to do it/seen it done successfully?

    Jonas

  •  04-05-2006, 8:30 PM 126 in reply to 125

    Re: asp.net custom control

    Aint nothing that a simple attribute wouldn't fix.
    Add the following to your control class and read about it
    [ParseChildren(false)]
  •  04-05-2006, 8:31 PM 127 in reply to 126

    Re: asp.net custom control

    pooh:
    Aint nothing that a simple attribute wouldn't fix.
    Add the following to your control class and read about it
    [ParseChildren(false)]


    Stupid test accounts!!! That was from me. :)
  •  04-06-2006, 9:18 AM 129 in reply to 127

    Re: asp.net custom control

    A slight additional note, if you are using a custom .Render override, you should call the base.RenderChildren(writer); method...  Have done this a bit with fancier section wrappers...
  •  04-06-2006, 9:35 AM 130 in reply to 125

    Re: asp.net custom control

    As an aside...I can't seem to configure 2.0 to use my server control.

    I have TestServerControl.cs in the App_Code directory. In my default.aspx I have the following register directive:

    <%@ Register tagprefix="cc1" src="App_Code/TestServerControl.cs" tagname="foo" %>

    When I try and compile, I get this error:

    Error 1 The file '/newatlasweb/App_Code/TestServerControl.cs' is in the special directory 'App_Code', which is not allowed.

  •  04-06-2006, 9:54 AM 131 in reply to 130

    Re: asp.net custom control

    JPrime:

    As an aside...I can't seem to configure 2.0 to use my server control.

    I have TestServerControl.cs in the App_Code directory. In my default.aspx I have the following register directive:

    <%@ Register tagprefix="cc1" src="App_Code/TestServerControl.cs" tagname="foo" %>

    When I try and compile, I get this error:

    Error 1 The file '/newatlasweb/App_Code/TestServerControl.cs' is in the special directory 'App_Code', which is not allowed.



    I admit that I don't know much about the code layout of asp.net 2.0 projects. With that said, if you are creating custom server controls, why not keep them in a seperate class library?
  •  04-06-2006, 10:25 AM 132 in reply to 131

    Re: asp.net custom control

    Ahh the joys of new stuff. :) In 2.0, if you have the server control in your app_code directory, it must have a namespace, then the register directive should look like this:

    <%@ register namespace="TestWebControls" tagprefix="testControl" %>

    Then in your markup:

    <testControl:TestServerControl cssclass="Foo" id="foo1" runat="server">
       inside
    </testControl:TestServerControl>

    the "TestServerControl" name must match the name of the class of your custom server control (which I overlooked...whoops). You can also register the control in your web.config for ease of use in multiple pages:

    <system.web>
       <
    pages>
          <
    controls>
             <add namespace="TestWebControls" tagPrefix="testControl" />
          </
    controls>
       </
    pages>
    </system.web>

    Since this control is only going to be used in this app, I don't need to put it in a separate dll. But if you do put it in a separate dll, all you need to add is the "assembly" attribute when registering your controls.

    --j

  •  04-06-2006, 12:22 PM 134 in reply to 132

    Re: asp.net custom control

    Yes.  Further, 2.0 recognizes "App_Code" as a very special directory that gets compiled.  ASP.NET will compile your class and make it available to you from the assembly inside the bin/ directory.  Keep in mind that the "App_Code" directory is often not included in the final website... just the dlls in the bin/ directory.
    --Eric Swanson
    http://www.ericis.com/
  •  11-14-2006, 7:24 AM 664 in reply to 132

    Re: asp.net custom control

    signed up to say thanks for putting all this info in one easy-to-find spot.  definitely saved me some time searching the rest of google for all this.
  •  03-21-2007, 3:06 PM 752 in reply to 126

    Re: asp.net custom control

    pooh:
    Aint nothing that a simple attribute wouldn't fix.
    Add the following to your control class and read about it
    [ParseChildren(false)]

     great feature!  I found this also when I went to make a Templated control..

    <foo:SomeControl ID="ucTest" runat="server">

         <MainContent>Render me Inside a specific placeholder!</MainContent>

         <Header>Snappy and informative header!</Header>

    </foo:SomeControl>

     

    Requires something like..

    private ITemplate _MainContent;

    [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))]

    public ITemplate MainContent

    {

    get { return _MainContent; }

    set { _MainContent = value; }

    }

     

    etc..etc...

    private ITemplate _MainContent;

    [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))]

    public ITemplate MainContent

    {

    get { return _MainContent; }

    set { _MainContent = value; }

    }

     

    etc..etc...

    PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))]

    public ITemplate MainContent

    {

    get { return _MainContent; }

    set { _MainContent = value; }

    }

     

    etc..etc...

    public ITemplate MainContent

    {

    get { return _MainContent; }

    set { _MainContent = value; }

    }

     

    etc..etc...

    get { return _MainContent; }

    set { _MainContent = value; }

    }

     

    etc..etc...

    set { _MainContent = value; }

    }

     

    etc..etc...

  •  03-21-2007, 3:09 PM 753 in reply to 752

    Re: asp.net custom control

    eck gawd!  not sure why that repeated a billion times..   alas.. no EDIT POST option.. sorry..  but the gist is, if oyu want Templates, you need to expose ITemplate properties and then override OnInit.  Like so:

    protected override void OnInit(EventArgs e)

    {

    base.OnInit(e);

    if (_MainContent != null)

    {

    _MainContent.InstantiateIn(Content);

    }

    }

    protected override void OnInit(EventArgs e)

    {

    base.OnInit(e);

    if (_MainContent != null)

    {

    _MainContent.InstantiateIn(Content);

    }

    }

    base.OnInit(e);

    if (_MainContent != null)

    {

    _MainContent.InstantiateIn(Content);

    }

    }

    if (_MainContent != null)

    {

    _MainContent.InstantiateIn(Content);

    }

    }

  • View as RSS news feed in XML
    Powered by Community Server, by Telligent Systems