473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Steve

Nov 19 '05 #1
2 1124
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" <st***@straker.co.nz> wrote in message
news:O$**************@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

Nov 19 '05 #2
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:uf**************@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" <st***@straker.co.nz> wrote in message
news:O$**************@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


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jim Quast | last post by:
I have an ASP page and a CREGReports002.vbs file coded to export data to excel. I do this by building variables in a stored procedure. The ASP page has text boxes, list boxes, and radio buttons. ...
21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
4
by: Gill Smith | last post by:
After setting the web control text box enable property to FALSE makes the control to loose the data between round trip. I am making sure that the property - EnableViewState = TRUE. Same code...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
1
by: simon | last post by:
I have an unbound RTF2 control in an Access 2003 form which I set to show either unformatted text or else text with words and sentences highlighted. I do this by setting the PlainText property,...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
7
by: Brad Pears | last post by:
I have something strange going on - pretty sure it used to work before - and now it does not... Why does the following code not clear a combo box? Me.cboLocation.Text = String.Empty OR ...
25
bugboy
by: bugboy | last post by:
I'm trying to find a cross browser solution for editing text in a flickr photo caption manor where the text is displayed in a <div> or <a> tag but changes to a <textarea> via JS when clicked. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.