Saturday, March 24, 2012

loop througth Literials using foreach!

In my web application i use satalite assembly to localize my web form..
Instead of set the each control text to the resourcemanager getstring()
method i try to use foreach loop
private string rmg(string key)
{
return resource.GetString("key");
}
private Literal ctrl_txt()
{
foreach (Literal lit_xyz in this.Controls)
{
string str= lit_xyz.ID;
string key= str.Substring(3);
lit_xyz.Text= rmg(key);
return lit_xyz;
}
return null;
}
this foreach genrate InvalidCastException... How can i fix this'foreach (Control control in Page.Controls)
{
if (control is Literal)
{
Literal lc = (Literal)control;
// Do what you want here with lc now.
}
}
HTH
DalePres
MCAD, MCDBA, MCSE
"Islam Elkhayat" <islamegy@.yahoo.com> wrote in message
news:O5Qgq6xDFHA.1496@.TK2MSFTNGP14.phx.gbl...
> In my web application i use satalite assembly to localize my web form..
> Instead of set the each control text to the resourcemanager getstring()
> method i try to use foreach loop
> private string rmg(string key)
> {
> return resource.GetString("key");
> }
> private Literal ctrl_txt()
> {
> foreach (Literal lit_xyz in this.Controls)
> {
> string str= lit_xyz.ID;
> string key= str.Substring(3);
> lit_xyz.Text= rmg(key);
> return lit_xyz;
> }
> return null;
> }
> this foreach genrate InvalidCastException... How can i fix this'
>
Islam Elkhayat wrote:
> In my web application i use satalite assembly to localize my web form..
> Instead of set the each control text to the resourcemanager getstring()
> method i try to use foreach loop
> private string rmg(string key)
> {
> return resource.GetString("key");
> }
> private Literal ctrl_txt()
> {
> foreach (Literal lit_xyz in this.Controls)
> {
> string str= lit_xyz.ID;
> string key= str.Substring(3);
> lit_xyz.Text= rmg(key);
> return lit_xyz;
> }
> return null;
> }
> this foreach genrate InvalidCastException... How can i fix this'
>
your foreach doesn't mean "use only the Literals from
the Controls collection", it means "use every control
in Controls as if it were a Literal".
As you found out, that is not true.
The Controls collection consists of Controls that *might*
be Literals, so:
foreach (Control ctrl in this.Controls)
{
Literal lit = ctrl as Literal;
if (lit != null)
{
// work with the Literal
}
// else no literal, ignore it
}
Note: this foreach goes just one level deep, you don't find
"grandchildren" this way. If you want to find any Literal, no matter
how deep, you need to use recursion ("if this is not a Literal,
then maybe somewhere in the Controls collection of this control
there is a Literal")
Hans Kesting
It didn't throw Exception but also didn't work
if statment always false... so i always go to the else!!
How can i make it work'
How are you creating your literals? In my tests of your problem, I found
that Literals created in the aspx file as <asp:LITERAL> were not available
in code even if I made sure that they were declared and instantiated in my
codebehind.
In order to get the Literals to work, I had to create them, instantiate
them, and them to Page.Controls from within my codebehind. Done that way, I
was able to access the Literal and manipulate its properties.
"Islam Elkhayat" <islamegy@.yahoo.com> wrote in message
news:OAaOtI9DFHA.3244@.TK2MSFTNGP15.phx.gbl...
> It didn't throw Exception but also didn't work
> if statment always false... so i always go to the else!!
> How can i make it work'
>
I want to loop throught literials so i don't use
Lit_1.Text= resources.GetString(key)
for each literial... if i instantiated every literial in the code behind i
will write the same long code!!!
How can i solve this issue'
"DalePres" <don-t-spa-m-me@.lea-ve-me-a-lone--.com> wrote in message
news:O39xgU9DFHA.732@.TK2MSFTNGP12.phx.gbl...
> How are you creating your literals? In my tests of your problem, I found
> that Literals created in the aspx file as <asp:LITERAL> were not available
> in code even if I made sure that they were declared and instantiated in my
> codebehind.
> In order to get the Literals to work, I had to create them, instantiate
> them, and them to Page.Controls from within my codebehind. Done that way,
> I was able to access the Literal and manipulate its properties.
>
> "Islam Elkhayat" <islamegy@.yahoo.com> wrote in message
> news:OAaOtI9DFHA.3244@.TK2MSFTNGP15.phx.gbl...
>
On Thu, 10 Feb 2005 19:02:50 -0600, "DalePres"
<don-t-spa-m-me@.lea-ve-me-a-lone--.com> wrote:

>How are you creating your literals? In my tests of your problem, I found
>that Literals created in the aspx file as <asp:LITERAL> were not available
>in code even if I made sure that they were declared and instantiated in my
>codebehind.
>
If a class is instantiated - it's available. Perhaps you meant
declared? Are you using runat="server"?
Scott
http://www.OdeToCode.com/blogs/scott/
You're correct, if a class is instantiated it is available. But try it out.
If you create an <ASP:LITERAL runat="server" and id="Literal1" Text="My
Literal Text" /> and then, in your code behind make sure that you declare
and instantiate Literal1, they are not recognized as the same object. You
can access, modify, and do what you want with the one in your codebehind,
and it will behave as expected; the literal in your aspx file will always
render as "My Literal Text".
DalePres
"Scott Allen" <scott@.nospam.odetocode.com> wrote in message
news:p42o01l5141ce9g98maobgoprise7smaco@.
4ax.com...
> On Thu, 10 Feb 2005 19:02:50 -0600, "DalePres"
> <don-t-spa-m-me@.lea-ve-me-a-lone--.com> wrote:
>
> If a class is instantiated - it's available. Perhaps you meant
> declared? Are you using runat="server"?
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
If there is a literal control on the page, the control has to be
instantiated to render. Perhaps you meant you didn't want to declare a
variable for each Literal control?
In any case, I think a cleaner solution would be to create a new
control (pehaps derived from Literal or WebControl) and use the Init
event to get the string from a resource file. You could add Key as a
public property to be able to assign the key from inside the ASPX tag.
The above approach will be a cleaner, more maintainable solution.
You'll be able to use the new control on any page in your application
without cluttering eeach page with the string loading logic.
HTH,
Scott
http://www.OdeToCode.com/blogs/scott/
On Fri, 11 Feb 2005 03:13:07 +0200, "Islam Elkhayat"
<islamegy@.yahoo.com> wrote:

>I want to loop throught literials so i don't use
>Lit_1.Text= resources.GetString(key)
>for each literial... if i instantiated every literial in the code behind i
>will write the same long code!!!
>How can i solve this issue'
>
>"DalePres" <don-t-spa-m-me@.lea-ve-me-a-lone--.com> wrote in message
>news:O39xgU9DFHA.732@.TK2MSFTNGP12.phx.gbl...
>
You can read my blog on strongly typed localization features built into
Visual Studio 2005 at
http://www.dalepreston.com/Blog/Arc...9_Archive.html. While the
classes required are created automatically, in Visual Studio 2005, by the
/str switch of the ResGen.exe program, you could generate similar classes
yourself in code.
You could write a strongly typed resource class generator of your own to
read the resource xml key/value pairs. Here's an interesting article that
might help you get started: http://www.thecodeproject.com/csharp/config.asp.
Something you might try is using the VS2005 version of ResGen.exe and create
the strongly typed classes and try them in VS2003. Based on what I've seen,
I think it would work seamlessly or nearly so.
DalePres
"Islam Elkhayat" <islamegy@.yahoo.com> wrote in message
news:e6rShb9DFHA.4004@.tk2msftngp13.phx.gbl...
>I want to loop throught literials so i don't use
> Lit_1.Text= resources.GetString(key)
> for each literial... if i instantiated every literial in the code behind i
> will write the same long code!!!
> How can i solve this issue'
>
> "DalePres" <don-t-spa-m-me@.lea-ve-me-a-lone--.com> wrote in message
> news:O39xgU9DFHA.732@.TK2MSFTNGP12.phx.gbl...
>

0 comments:

Post a Comment