CssClass property not recognized by Inherited WebControl?

D

Dave

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.
 
V

Victor Garcia Aprea [MVP]

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 said:
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 said:
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?
 
D

Dave

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 said:
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 said:
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 said:
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.
 
V

Victor Garcia Aprea [MVP]

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 said:
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 said:
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 said:
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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top