Help with Webforms component inheritance

S

Strange Cat

Hi everyone!

I am trying to write some "auto-translating" components for an ASP.NET
intranet application.

I started inheriting from label and a made a small control. Also i tested on
the button, they work OK, but I have a question: here is the short code for
the btwButton i made:

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Resources

<DefaultProperty("Text"), ToolboxData("<{0}:btwButton
runat=server></{0}:btwButton>")> Public Class btwButton
Inherits System.Web.UI.WebControls.Button

Private Function TranslateText(ByVal toTranslate As String) As String
Dim r As ResourceManager

Try
r = Page.Application("Translator")
Return r.GetString(toTranslate)
Catch ex As System.Exception
Return toTranslate
End Try
End Function

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
[Text] = TranslateText([Text]) <<<<< [Text] changes are lost
MyBase.Render(output)
End Sub
End Class

It works as I want, but i'd like to know why the Text property changes after
the first Render are lost, and the next time it renders the control it
returns to the Text property i set in design mode. To make it clear, i'm
happy with this behaviour, but I'd like to know why the Text property gets
resetted to the designtime value... Just in case i'm doing something real
wrong, i'm new to inheriting controls and so on...

TIA

ASC
 
J

John Saunders

Strange Cat said:
Hi everyone!

I am trying to write some "auto-translating" components for an ASP.NET
intranet application.

I started inheriting from label and a made a small control. Also i tested on
the button, they work OK, but I have a question: here is the short code for
the btwButton i made:

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Resources

<DefaultProperty("Text"), ToolboxData("<{0}:btwButton
runat=server></{0}:btwButton>")> Public Class btwButton
Inherits System.Web.UI.WebControls.Button

Private Function TranslateText(ByVal toTranslate As String) As String
Dim r As ResourceManager

Try
r = Page.Application("Translator")
Return r.GetString(toTranslate)
Catch ex As System.Exception
Return toTranslate
End Try
End Function

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
[Text] = TranslateText([Text]) <<<<< [Text] changes are lost
MyBase.Render(output)
End Sub
End Class

It works as I want, but i'd like to know why the Text property changes after
the first Render are lost, and the next time it renders the control it
returns to the Text property i set in design mode. To make it clear, i'm
happy with this behaviour, but I'd like to know why the Text property gets
resetted to the designtime value... Just in case i'm doing something real
wrong, i'm new to inheriting controls and so on...

Render is too late to make changes like that. Try making the change in
PreRender.
 
S

Strange Cat

[CUT]
Render is too late to make changes like that. Try making the change in
PreRender.

Thanx for answering, John. I'm happy with the behaviour of my very small
component, as I need the text to be resetted like that. I was just thinking
if it is legal to change Text property in the Render event handler, well if
it works... it must be legal, I guess.

Thanx

ASC
 
J

John Saunders

Strange Cat said:
[CUT]
Render is too late to make changes like that. Try making the change in
PreRender.

Thanx for answering, John. I'm happy with the behaviour of my very small
component, as I need the text to be resetted like that. I was just thinking
if it is legal to change Text property in the Render event handler, well if
it works... it must be legal, I guess.

BTW, "if it works it must be legal" is a bad assumption. It may only seem to
work (meaning you haven't tested for the cases where it doesn't work), or it
may only work in this release and won't work in the next release.
 
S

Strange Cat

BTW, "if it works it must be legal" is a bad assumption. It may only seem
to
work (meaning you haven't tested for the cases where it doesn't work), or it
may only work in this release and won't work in the next release.

I guess you're right...

So i have tried also overriding the OnPrerender event in my small components
and the behaviour is the same, i can change the text property there, the
control gets rendered with the new text property, but on the next
OnPrerender event, the text property returns as I set it at design time...
still have to figure out why, but, again that's what I wanted.

Thanx John for answering

ASC
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top