Saturday, March 24, 2012

Setting Text Value

Hi,
I'm parsing every Button / Label / hyperlink that is on my web forms so that
I can look at the text values and translate them. I used to do this in
VB.Net by casting everything as an object (Adapting the page inheritance
model by Johathon Goodyear):
As shown:
CType(myControl, Object).text = TranslatedVersion(CType(myControl,
Object).text)
However, If I try to do something similar with C# this code doesn't work and
I get a compile error.
i.e. ((System.Web.UI.WebControls.WebControl)myControl).Text
Any ideas of what I need to do for a similar effect in C#?
Thanks for any help.
SteveSteve:
You can't without using reflection. The only reason you could in VB.net was
because you had Option Strict turned off instead of On. Your code was
using what's called "late-binding" which is (a) slow (b) not very readable
and (c) more likely to give a run-time error. There are certainly times
when late-binding is a necessary feature, but my personal opinion is that I
like how hard C# makes it because them it makes you think "is this really
the best way to do it?!"...most of the time you'll hopefully find out that
it isn't...
You could do:
Type type = myControl.GetType();
PropertyInfo pi = type.GetProperty("Text");
if (pi != null){
pi.SetValue(myControl, TranslatedVersion(pi.GetValue(myControl, null)),
null);
}
or something similar...
Dunno what you are doing with translation, but you might wanna take a look
at:
http://openmymind.net/index.aspx?documentId=3
and
http://openmymind.net/index.aspx?documentId=4
Karl
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Steve Letford" <steve@.straker.co.nz> wrote in message
news:O$qKPTsMFHA.1392@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I'm parsing every Button / Label / hyperlink that is on my web forms so
that
> I can look at the text values and translate them. I used to do this in
> VB.Net by casting everything as an object (Adapting the page inheritance
> model by Johathon Goodyear):
> As shown:
> CType(myControl, Object).text = TranslatedVersion(CType(myControl,
> Object).text)
> However, If I try to do something similar with C# this code doesn't work
and
> I get a compile error.
> i.e. ((System.Web.UI.WebControls.WebControl)myControl).Text
> Any ideas of what I need to do for a similar effect in C#?
> Thanks for any help.
> Steve
>
>
Thanks Karl,
Worked a treat, and thanks for the links for the resource info.
Steve
"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:ufWhkRtMFHA.1500@.TK2MSFTNGP09.phx.gbl...
> Steve:
> You can't without using reflection. The only reason you could in VB.net
> was
> because you had Option Strict turned off instead of On. Your code was
> using what's called "late-binding" which is (a) slow (b) not very readable
> and (c) more likely to give a run-time error. There are certainly times
> when late-binding is a necessary feature, but my personal opinion is that
> I
> like how hard C# makes it because them it makes you think "is this really
> the best way to do it?!"...most of the time you'll hopefully find out that
> it isn't...
> You could do:
> Type type = myControl.GetType();
> PropertyInfo pi = type.GetProperty("Text");
> if (pi != null){
> pi.SetValue(myControl, TranslatedVersion(pi.GetValue(myControl, null)),
> null);
> }
> or something similar...
> Dunno what you are doing with translation, but you might wanna take a look
> at:
> http://openmymind.net/index.aspx?documentId=3
> and
> http://openmymind.net/index.aspx?documentId=4
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
>
> "Steve Letford" <steve@.straker.co.nz> wrote in message
> news:O$qKPTsMFHA.1392@.TK2MSFTNGP10.phx.gbl...
> that
> and
>

0 comments:

Post a Comment