Id and CssClass not defined
Id and CssClass not defined
am 28.12.2007 19:27:46 von Shapper
Hello,
I created a custom control that inherits CompositeControl:
Public Class MySection
Inherits CompositeControl
...
Protected Overrides Sub CreateChildControls()
...
MyBase.CreateChildControls()
Me.ChildControlsCreated = True
End Sub
End Class MySection
When I use this control I define its ID and CssClass.
However, when I check the rendered output the ID and CssClass are not
defined.
What am I doing wrong?
Shoudn't this be working, since I am inheriting my control from
CompositeControl?
Thanks,
Miguel
RE: Id and CssClass not defined
am 30.12.2007 02:12:01 von mily242
Hi Shapper,
HTML output should be rendered as
class="yourclass">. In addition, there's no need for setting
ChildControlsCreated to true because EnsureChildControls method does it for
you.
I'm guessing something else is causing problem, paste all the code.
--
Milosz
"shapper" wrote:
> Hello,
>
> I created a custom control that inherits CompositeControl:
>
> Public Class MySection
> Inherits CompositeControl
> ...
> Protected Overrides Sub CreateChildControls()
> ...
> MyBase.CreateChildControls()
> Me.ChildControlsCreated = True
> End Sub
>
> End Class MySection
>
> When I use this control I define its ID and CssClass.
> However, when I check the rendered output the ID and CssClass are not
> defined.
> What am I doing wrong?
>
> Shoudn't this be working, since I am inheriting my control from
> CompositeControl?
>
> Thanks,
> Miguel
>
Re: Id and CssClass not defined
am 30.12.2007 03:01:21 von Shapper
On Dec 30, 1:12 am, Milosz Skalecki [MCAD]
wrote:
> Hi Shapper,
>
> HTML output should be rendered as
> class="yourclass">. In addition, there's no need for setting
> ChildControlsCreated to true because EnsureChildControls method does it for
> you.
> I'm guessing something else is causing problem, paste all the code.
> --
> Milosz
>
> "shapper" wrote:
> > Hello,
>
> > I created a custom control that inherits CompositeControl:
>
> > Public Class MySection
> > Inherits CompositeControl
> > ...
> > Protected Overrides Sub CreateChildControls()
> > ...
> > MyBase.CreateChildControls()
> > Me.ChildControlsCreated = True
> > End Sub
>
> > End Class MySection
>
> > When I use this control I define its ID and CssClass.
> > However, when I check the rendered output the ID and CssClass are not
> > defined.
> > What am I doing wrong?
>
> > Shoudn't this be working, since I am inheriting my control from
> > CompositeControl?
>
> > Thanks,
> > Miguel
Hi,
I think I found the problem. In my custom control I also have the
following:
Public Overloads Overrides Sub RenderBeginTag(ByVal writer As
HtmlTextWriter)
writer.RenderBeginTag(HtmlTextWriterTag.Div)
End Sub ' RenderBeginTag
Public Overloads Overrides Sub RenderEndTag(ByVal writer As
HtmlTextWriter)
writer.RenderEndTag()
End Sub ' RenderEndTag
I changed it to:
Public Overloads Overrides Sub RenderBeginTag(ByVal writer As
HtmlTextWriter)
With writer
If Me.ID <> Nothing
Then .AddAttribute(HtmlTextWriterAttribute.Id, Me.ID)
If Me.CssClass <> Nothing
Then .AddAttribute(HtmlTextWriterAttribute.Class, Me.CssClass)
End With
With writer
If Me.Width <> Nothing
Then .AddAttribute(HtmlTextWriterAttribute.Style, "width: " &
Me.Width.ToString)
End With
' Render begin tag
writer.RenderBeginTag(HtmlTextWriterTag.Div)
End Sub ' RenderBeginTag
Now it is working.
Am I doing this right? Do I really need to do this?
Thanks,
Miguel
Re: Id and CssClass not defined
am 30.12.2007 03:48:02 von mily242
You don't need to bacause cssclass and id are rendered automatically. In
order to change default span tag to div, override TagKey property:
protected readonly overrides property TagKey() as HtmlTextWriterTag
get
return HtmlTextWriterTag.Div
end get
end property
hope this helps
--
Milosz
"shapper" wrote:
> On Dec 30, 1:12 am, Milosz Skalecki [MCAD]
> wrote:
> > Hi Shapper,
> >
> > HTML output should be rendered as
> > class="yourclass">. In addition, there's no need for setting
> > ChildControlsCreated to true because EnsureChildControls method does it for
> > you.
> > I'm guessing something else is causing problem, paste all the code.
> > --
> > Milosz
> >
> > "shapper" wrote:
> > > Hello,
> >
> > > I created a custom control that inherits CompositeControl:
> >
> > > Public Class MySection
> > > Inherits CompositeControl
> > > ...
> > > Protected Overrides Sub CreateChildControls()
> > > ...
> > > MyBase.CreateChildControls()
> > > Me.ChildControlsCreated = True
> > > End Sub
> >
> > > End Class MySection
> >
> > > When I use this control I define its ID and CssClass.
> > > However, when I check the rendered output the ID and CssClass are not
> > > defined.
> > > What am I doing wrong?
> >
> > > Shoudn't this be working, since I am inheriting my control from
> > > CompositeControl?
> >
> > > Thanks,
> > > Miguel
>
> Hi,
>
> I think I found the problem. In my custom control I also have the
> following:
>
> Public Overloads Overrides Sub RenderBeginTag(ByVal writer As
> HtmlTextWriter)
> writer.RenderBeginTag(HtmlTextWriterTag.Div)
> End Sub ' RenderBeginTag
> Public Overloads Overrides Sub RenderEndTag(ByVal writer As
> HtmlTextWriter)
> writer.RenderEndTag()
> End Sub ' RenderEndTag
>
> I changed it to:
>
> Public Overloads Overrides Sub RenderBeginTag(ByVal writer As
> HtmlTextWriter)
>
> With writer
> If Me.ID <> Nothing
> Then .AddAttribute(HtmlTextWriterAttribute.Id, Me.ID)
> If Me.CssClass <> Nothing
> Then .AddAttribute(HtmlTextWriterAttribute.Class, Me.CssClass)
> End With
> With writer
> If Me.Width <> Nothing
> Then .AddAttribute(HtmlTextWriterAttribute.Style, "width: " &
> Me.Width.ToString)
> End With
>
> ' Render begin tag
> writer.RenderBeginTag(HtmlTextWriterTag.Div)
>
> End Sub ' RenderBeginTag
>
> Now it is working.
>
> Am I doing this right? Do I really need to do this?
>
> Thanks,
> Miguel
>