CssClass property not recognized by Inherited WebControl?

Discussion in 'ASP .Net Building Controls' started by Dave, Jun 21, 2004.

  1. Dave

    Dave Guest

    Hi,

    I'm trying to create a custom Linkbutton control by inheriting from WebControls

    [ToolboxData("<{0}:MyLinkButtonrunat=server></{0}:MyLinkButton>")]
    [ParseChildren(false)]
    public class MyLinkButton: WebControl , IPostBackEventHandler
    .....

    I define my control in the aspx as:

    <CustomControls:MyLinkButton id="lbtnGetData" CssClass="LinkButton" runat="server" >View Data</CustomControls:MyLinkButton>

    However, the value in CssClass seems to be ignored and "class=" is not outputed to the HTML? Is there something else I need to implement?

    Any help would be appreciated..Thanks, Dave.
     
    Dave, Jun 21, 2004
    #1
    1. Advertising

  2. Hi Dave,

    My guess is that you're overriding Render and breaking WebControl's own
    rendering thus causing that property to not be properly rendered.
    If you could post some code, that would really help in understanding whats
    going on,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    "Dave" <> wrote in message
    news:D...
    > Hi,
    >
    > I'm trying to create a custom Linkbutton control by inheriting from

    WebControls
    >
    > [ToolboxData("<{0}:MyLinkButtonrunat=server></{0}:MyLinkButton>")]
    > [ParseChildren(false)]
    > public class MyLinkButton: WebControl , IPostBackEventHandler
    > .....
    >
    > I define my control in the aspx as:
    >
    > <CustomControls:MyLinkButton id="lbtnGetData" CssClass="LinkButton"

    runat="server" >View Data</CustomControls:MyLinkButton>
    >
    > However, the value in CssClass seems to be ignored and "class=" is not

    outputed to the HTML? Is there something else I need to implement?
    >
    > Any help would be appreciated..Thanks, Dave.
    >
    >
    >
    >
    >
    >
     
    Victor Garcia Aprea [MVP], Jun 22, 2004
    #2
    1. Advertising

  3. Dave

    Dave Guest

    Victor,

    Thanks for the reply! It seems that's what I was doing but I'm not out of the woods yet. I'm creating a custom LinkButton to add some javascript after the postback by writing out the "<a>" tag contents below.

    By adding base.Render(writer) last, it displays the "class" etc that I was missing before, but now it's appears within "<span>". How to do "inject" my custom "<a>" tag into the writer...Thanks again. Dave.


    public class LinkButtonFromWebControl : System.Web.UI.WebControls.WebControl

    protected override void Render(HtmlTextWriter writer)
    {
    string sOutPut = "<a id=\"" + this.ClientID + "\" href=\"javascript:__doPostBack('" + this.ClientID + "', '');alert('run some javascript here);\">" + LinkText + "<a/>";

    output.Write(sOutPut); //this writes out the link I created, but with no "class="

    base.Render(writer); //this will add the "class=" as specified in design time but will will be span tag instead of an anchor tag.

    I need to some how combine these two lines...

    }

    "Victor Garcia Aprea [MVP]" wrote:

    > Hi Dave,
    >
    > My guess is that you're overriding Render and breaking WebControl's own
    > rendering thus causing that property to not be properly rendered.
    > If you could post some code, that would really help in understanding whats
    > going on,
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    >
    > "Dave" <> wrote in message
    > news:D...
    > > Hi,
    > >
    > > I'm trying to create a custom Linkbutton control by inheriting from

    > WebControls
    > >
    > > [ToolboxData("<{0}:MyLinkButtonrunat=server></{0}:MyLinkButton>")]
    > > [ParseChildren(false)]
    > > public class MyLinkButton: WebControl , IPostBackEventHandler
    > > .....
    > >
    > > I define my control in the aspx as:
    > >
    > > <CustomControls:MyLinkButton id="lbtnGetData" CssClass="LinkButton"

    > runat="server" >View Data</CustomControls:MyLinkButton>
    > >
    > > However, the value in CssClass seems to be ignored and "class=" is not

    > outputed to the HTML? Is there something else I need to implement?
    > >
    > > Any help would be appreciated..Thanks, Dave.
    > >
    > >
    > >
    > >
    > >
    > >

    >
    >
    >
     
    Dave, Jun 22, 2004
    #3
  4. Dave,

    By what you have described you don't really need to override the Render method. You should be fine by just override RenderContents and using WebControl base ctor to specify the tag for your control is an anchor, i.e.:

    [C#]
    public class MyLinkButton : System.Web.UI.WebControls.WebControl
    {
    MyLinkButton () : base (HtmlTextWriterTag.A) {}

    protected override void RenderContents(HtmlTextWriter writer) {

    }

    }

    If you want to add attributes to the opening tag then you should override AddAttributesToRender method.

    This may seem more complex than just overriding Render and doing all the stuff in there, but its really not and its a much cleaner way to accomplish what you want.

    Hope that clears thing a bit and let me know if you have any other questions,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx


    "Dave" wrote:

    > Victor,
    >
    > Thanks for the reply! It seems that's what I was doing but I'm not out of the woods yet. I'm creating a custom LinkButton to add some javascript after the postback by writing out the "<a>" tag contents below.
    >
    > By adding base.Render(writer) last, it displays the "class" etc that I was missing before, but now it's appears within "<span>". How to do "inject" my custom "<a>" tag into the writer...Thanks again. Dave.
    >
    >
    > public class LinkButtonFromWebControl : System.Web.UI.WebControls.WebControl
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > string sOutPut = "<a id=\"" + this.ClientID + "\" href=\"javascript:__doPostBack('" + this.ClientID + "', '');alert('run some javascript here);\">" + LinkText + "<a/>";
    >
    > output.Write(sOutPut); //this writes out the link I created, but with no "class="
    >
    > base.Render(writer); //this will add the "class=" as specified in design time but will will be span tag instead of an anchor tag.
    >
    > I need to some how combine these two lines...
    >
    > }
    >
    > "Victor Garcia Aprea [MVP]" wrote:
    >
    > > Hi Dave,
    > >
    > > My guess is that you're overriding Render and breaking WebControl's own
    > > rendering thus causing that property to not be properly rendered.
    > > If you could post some code, that would really help in understanding whats
    > > going on,
    > >
    > > --
    > > Victor Garcia Aprea
    > > Microsoft MVP | ASP.NET
    > > Looking for insights on ASP.NET? Read my blog:
    > > http://obies.com/vga/blog.aspx
    > >
    > > "Dave" <> wrote in message
    > > news:D...
    > > > Hi,
    > > >
    > > > I'm trying to create a custom Linkbutton control by inheriting from

    > > WebControls
    > > >
    > > > [ToolboxData("<{0}:MyLinkButtonrunat=server></{0}:MyLinkButton>")]
    > > > [ParseChildren(false)]
    > > > public class MyLinkButton: WebControl , IPostBackEventHandler
    > > > .....
    > > >
    > > > I define my control in the aspx as:
    > > >
    > > > <CustomControls:MyLinkButton id="lbtnGetData" CssClass="LinkButton"

    > > runat="server" >View Data</CustomControls:MyLinkButton>
    > > >
    > > > However, the value in CssClass seems to be ignored and "class=" is not

    > > outputed to the HTML? Is there something else I need to implement?
    > > >
    > > > Any help would be appreciated..Thanks, Dave.
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >

    > >
    > >
    > >
     
    Victor Garcia Aprea [MVP], Jun 22, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    1
    Views:
    1,343
    Nathan Sokalski
    Jul 31, 2009
  2. Dales

    CssClass property not working

    Dales, Nov 19, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    175
    Dales
    Nov 19, 2003
  3. Sky
    Replies:
    2
    Views:
    285
    Robert Koritnik
    Oct 1, 2004
  4. Peter Morris [Air Software Ltd]

    WebControl with non-WebControl property

    Peter Morris [Air Software Ltd], Dec 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    133
    Peter Morris [Air Software Ltd]
    Dec 7, 2004
  5. Nathan Sokalski
    Replies:
    1
    Views:
    1,018
    Nathan Sokalski
    Jul 31, 2009
Loading...

Share This Page