Greets, When I inherit from WebControl and place the control wherever I want in the IDE (in Grid layout) all is well. When I view that page in a browser, however, the control is always at the top left corner of the screen. I'm presume it's because I'm not setting
the "style" to appropriately reflect absolute positioning and the top and left coordinates. Saying "left = Style["left"] doesn't return anything and I don't see any other properties or methods that provide this functionality. The WebControl itself doesn't
appear to provide this functionality, either. How to I get those cordinates from the IDE so I can write them in the final HTML stream to be rendered? Thanks, Shawn
Are you perhaps overriding AddAttributesToRender() ? That's where the style="position: absolute...." would be added. If you are overriding it, you can always add:
Will this cause the left and top styles to be rendered correctly as they are positioned in the IDE? I will have to look into this later I'm not near that project at the moment... but if it will cause those styles to be rendered then that's just dandy... =))
Thanks, Shawn
So by overriding AddAttributesToRender of the WebControl, you can customize what attributes it will
output. Call to base class's version ensures that design-time placing works and it is reflected in the rendering.
Okay, what you say makes sense but I must be missing something here... when I create a new webcontrol and override addattributestorender... the output doesn't add the stream nor is it accessible without throwing an exception... ")> Public Class TestControl
Inherits System.Web.UI.WebControls.WebControl Dim _text As String Property [Text]() As String Get Return _text End Get Set(ByVal Value As String) _text = Value End Set End Property Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
writer.AddStyleAttribute("position", "absolute") MyBase.AddAttributesToRender(writer) End Sub Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter) 'output.Write(MyBase.Style.Item("position").ToString) ' Fails output.Write([Text]) End
Sub End Class So I must be missing something here because it's still rendering in the top left no matter where it's positioned... now, in reality, the control in question (that I'm working on) will be rendering a table that needs to be positioned. Any ideas?
Thanks, Shawn
Hi, remove the overridden AddAttributesToRender and Render methods completely from the control as by default they should work just fine for your control's purposes. Instead override the RenderContents method of your control:
Protected Overrides Sub RenderContents(ByVal output As System.Web.UI.HtmlTextWriter)
output.Write([Text])
End Sub
Idea is that RenderContents
is called when your control want's to Render it's contents (Text/markup it contains).
Great, That worked (after I removed Render)... it places a .. tag around whatever is rendered to the screen. Lets take this one step further (if you don't mind?)... when I place a TextBox control on a form it renders <input ... style='left:...."....
etc... and doesn't wrap it in a span. My control will ultimately be rendered in a ... how would I go about causing such a behavior... PS: thanks for your help thus far, I've been stuck for a very long time on this. Thanks, Shawn
Yes, correct. That is because by default WebControl renders SPAN tag. Say if you'd like to write out an table with one row and cell where the text resides. Quickly thinking you'd have three options for doing it. 1) Set the TagKey property of your control to
point the "main" tag that the control is rendered as. Render the contents in RenderContents method. Example:
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
")> Public Class TestControl
Inherits System.Web.UI.WebControls.WebControl, INamingContainer
Dim _text As String
Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Table
End Get
End Property
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.Write(Text)
writer.RenderEndTag()
writer.RenderEndTag()
End Sub
End Class
2. Write it all out in Render method. This would mean applying AddAttributesToRender (because if we'd call base class's implementation, we would get the
also). Example: Example:
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
")> Public Class TestControl
Inherits System.Web.UI.WebControls.WebControl
Dim _text As String
Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
AddAttributesToRender(writer)
writer.RenderBeginTag(HtmlTextWriterTag.Table)
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.Write(Text)
writer.RenderEndTag()
writer.RenderEndTag()
writer.RenderEndTag()
End Sub
End Class
3) Create your control as composite control, and create the table by adding corresponding controls (Table,TableRow,TableCell) into your control's child controls and render them out in Render method of your control. Example:
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
")> Public Class TestControl
Inherits System.Web.UI.WebControls.WebControl
Dim _text As String
Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Dim t As New Table
Controls.Add(t)
Dim tr As New TableRow
Dim td As New TableCell
td.Text = Text
tr.Cells.Add(td)
t.Rows.Add(tr)
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
AddAttributesToRender(writer)
RenderChildren(writer)
End Sub
End Class
Composition might seem the easiest way to do the task, but it is also the less performatic, because it includes the extra control creation, state management and so on.
Good reply, I'll see what I can do... you've been helpful... thanks... just one more question... In all of this process (as long as it works I'll be happy -- and it looks to be the solution)... is there a way I can (in the source code of my webcontrol) access
the what the top and left and some other style properties that will eventually be written into the html writer? Or is it that I can't know but somehow (as you described above) it'll just get rendered in the end? Thanks, Shawn
Ah, yes that is the task of the AddAttributesToRender method. It will output automatically those that are applied to WebControl (via properties/styles of the control) and also if you put there custom attributes (so-called expando attributes) that are outputted
as well. For example moving the WebControl in the design view has impact on this (it effects top/left styles). So if you meant how to have effect on these on the control itself, modify control's proeprties as you find the best. And you could add your own attributes
in the AddAttributesToRender method if you override it (like you did in your first example).
leabre
Member
700 Points
140 Posts
Can't Get Style
May 21, 2003 04:07 PM|LINK
Craig Guttor...
Member
55 Points
11 Posts
Re: Can't Get Style
May 21, 2003 05:48 PM|LINK
-- This behaviour is by design.
leabre
Member
700 Points
140 Posts
Re: Can't Get Style
May 21, 2003 08:09 PM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: Can't Get Style
May 21, 2003 09:03 PM|LINK
public virtual void RenderBegintag(HtmlTextWriter writer) { AddAttributesToRender(writer) HtmlTextWriterTag tagKey=TagKey; ... }So by overriding AddAttributesToRender of the WebControl, you can customize what attributes it will output. Call to base class's version ensures that design-time placing works and it is reflected in the rendering.Teemu Keiski
Finland, EU
leabre
Member
700 Points
140 Posts
Re: Can't Get Style
May 21, 2003 09:26 PM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: Can't Get Style
May 21, 2003 09:31 PM|LINK
Teemu Keiski
Finland, EU
leabre
Member
700 Points
140 Posts
Re: Can't Get Style
May 21, 2003 09:45 PM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: Can't Get Style
May 21, 2003 10:04 PM|LINK
Imports System Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls ")> Public Class TestControl Inherits System.Web.UI.WebControls.WebControl, INamingContainer Dim _text As String Property [Text]() As String Get Return _text End Get Set(ByVal Value As String) _text = Value End Set End Property Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag Get Return HtmlTextWriterTag.Table End Get End Property Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter) writer.RenderBeginTag(HtmlTextWriterTag.Tr) writer.RenderBeginTag(HtmlTextWriterTag.Td) writer.Write(Text) writer.RenderEndTag() writer.RenderEndTag() End Sub End Class2. Write it all out in Render method. This would mean applying AddAttributesToRender (because if we'd call base class's implementation, we would get the also). Example: Example:Imports System Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls ")> Public Class TestControl Inherits System.Web.UI.WebControls.WebControl Dim _text As String Property [Text]() As String Get Return _text End Get Set(ByVal Value As String) _text = Value End Set End Property Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) AddAttributesToRender(writer) writer.RenderBeginTag(HtmlTextWriterTag.Table) writer.RenderBeginTag(HtmlTextWriterTag.Tr) writer.RenderBeginTag(HtmlTextWriterTag.Td) writer.Write(Text) writer.RenderEndTag() writer.RenderEndTag() writer.RenderEndTag() End Sub End Class3) Create your control as composite control, and create the table by adding corresponding controls (Table,TableRow,TableCell) into your control's child controls and render them out in Render method of your control. Example:Imports System Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls ")> Public Class TestControl Inherits System.Web.UI.WebControls.WebControl Dim _text As String Property [Text]() As String Get Return _text End Get Set(ByVal Value As String) _text = Value End Set End Property Protected Overrides Sub CreateChildControls() Dim t As New Table Controls.Add(t) Dim tr As New TableRow Dim td As New TableCell td.Text = Text tr.Cells.Add(td) t.Rows.Add(tr) End Sub Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) AddAttributesToRender(writer) RenderChildren(writer) End Sub End ClassComposition might seem the easiest way to do the task, but it is also the less performatic, because it includes the extra control creation, state management and so on.Teemu Keiski
Finland, EU
leabre
Member
700 Points
140 Posts
Re: Can't Get Style
May 21, 2003 10:11 PM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: Can't Get Style
May 21, 2003 10:17 PM|LINK
Teemu Keiski
Finland, EU