Category Archives: Localize

[RESOLVED] How do I to write a value to a Label in the Master Page

I'm trying to write a value to a Label in the Master Page called "Root.Master" in "MasterPages" folder.
I have this Label in the Master Page :

 <asp:Label ID="rootLabel" runat="server" />

I tried this in the OneVariant.ascx.cs user control :

 ((Label)Page.Master.FindControl("rootLabel")).Text = Server.HtmlEncode(product.LocalizedName);

but get " System.NullReferenceException: Object reference not set to an instance of an object."
How can I get this to work? thanks

 

 

Refer this link for your ans

http://forums.asp.net/t/1623329.aspx

Does it work if you split it into two lines?

Label rootLabel = (Label)Page.Master.FindControl("rootLabel");
rootLabel.Text = Server.HtmlEncode(product.LocalizedName);

If not, is the label contained within a template or other control?

Matt

thanks, . .  but that answer is in VB and I need C#.

 

 

Both of the answers provided are in C#.

 

Matt

If you ever need to convert from VB to C# or vice-versa, here is a good tool for you...

http://converter.telerik.com/

 

Matt

 

I tired all the solutions (in C#) there and they are not working.

Dim contentPlaceHolder As ContentPlaceHolder = DirectCast(Master.FindControl("cphBody"), ContentPlaceHolder)
Dim myControl As ControlType = DirectCast(contentPlaceHolder.FindControl("ControlName"),

Anything with "Dim" is VB.

 

yes

Hmm, I still don't see any Dim statements, but my eyes aren't what they used to be.  Is the label on the MasterPage contained within another control or template?

 

Matt

>>  Is the label on the MasterPage contained within another control or template?  No, just the <form> tag.

Hi,

According to your description, I will give you a sample below:

In the WebUserControl_master.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl_master.ascx.cs" Inherits="WebUserControl_master" %>

In the WebUserControl_master.ascx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        ((Label)Page.Master.FindControl("rootLabel")).Text ="changed";
    }

In the MasterPage_test.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage_test.master.cs" Inherits="MasterPage_test" %>
<%@ Register src="WebUserControl_master.ascx" tagname="WebUserControl_master" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
         <asp:Label ID="rootLabel" runat="server"  Text="no change!!!"/>        
        <uc1:WebUserControl_master ID="WebUserControl_master1" runat="server" />
         </div>
    </form>
</body>
</html>

In the master_usercontrol_master.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage_test.master" AutoEventWireup="true" CodeFile="master_usercontrol_master.aspx.cs" Inherits="master_usercontrol_master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

When you run the project above, the page will show “changed”. Because “((Label)Page.Master.FindControl("rootLabel")).Text ="changed";” has been put into the method Page_Load(). When the project run, the method Page_Load() will be fired and the value of rootLabel will change.

Modify it to meet your requirement.

thanks Catherine,

I got it to work by just adding this line in the content pages :

(Page.Master.Master.FindControl("rootLabel") as Label).Text = Server.HtmlEncode(product.LocalizedName);

 

[RESOLVED] Localization of strings other than in web form controls

I know that it is possible to localize web form controls by using a resource file, but what if I need to localize strings in the C# code itself. Example:

string str = "Hello World!";   //Need to be localized

How do you do that?

You simply refer to a resource file like this:

 

string str = ResourceFile.ResourceName;

Great, thank you!

Hi again. I was trying to use your solution and it doesn't seem to work for me. Here's what I have so far:

1. I added the following files into the App_LocalResources folder:

Default.aspx.resx
Default.aspx.fr.resx

2. Each of the Default.aspx.*.resx files has a single line with the Name set to Label1.Text and value "English", or "French". I can use those resource in the asp mark-up itself by adding meta:resourceKey="Label1"

3. But how do I access that string from the C# code? Default.Label1 doesn't seem to work like you said.

Found the solution:

string str = GetLocalResourceObject("Label1.Text").ToString();

[RESOLVED] Localization in the Master page

Hi everyone:

I was able to localize text in the pages of my web app by using the meta:resourceKey tag. Example:

<asp:Label ID="LabelLoc1" runat="server" Text="Label" meta:resourceKey="LabelLoc1"></asp:Label>

But the above approach doesn't seem to work in the master page.

How do I localize it then?

This thread has the answer to your problem http://forums.asp.net/t/1596825.aspx/

Regards

Sorry

We couldn't locate content with that identity.

http://forums.asp.net/t/1596825.aspx/ 

Try again or copy the adress and paste it in your browser. For me it works.

Regards

Thank you.

[RESOLVED] GetLocalResourceObject fails to compile

I'm trying to put the retrieval of localized strings from a .resx file into a global method that can be accessed from my web app. Here's what I came up with so far:

namespace WebApp_Demo
{
public class Globals
{
		public static string getLocalizedString(string strResID)
{
object obj = GetLocalResourceObject(strResID + ".Text");
return obj != null ? obj.ToString() : "";
}
}
}

But the code above doesn't compile. I get:
error CS0103: The name 'GetLocalResourceObject' does not exist in the current context

Try this.

namespace WebApp_Demo
{
    public class Globals
    {
		public static string getLocalizedString(string strResID)
		{
			object obj = HttpContext.GetLocalResourceObject(strResID + ".Text");
			return obj != null ? obj.ToString() : "";
		}
    }
}

Thanks. Now I'm very close. I get this error:

error CS1501: No overload for method 'GetLocalResourceObject' takes 1 arguments

The first argument needs to be string virtualPath. What is it?

ahmd01

error CS1501: No overload for method 'GetLocalResourceObject' takes 1 arguments

There are 2 overloads for this method. http://msdn.microsoft.com/en-us/library/system.web.httpcontext.getlocalresourceobject.aspx

For the first overload you have to give the virtual path and the resource key as parameters. Virtual path is the path to the file and resource key is the key or id of the particular resource.

Try this.

namespace WebApp_Demo
{
    public class Globals
    {
                public static string getLocalizedString(string strResID)
                {
                        string virtualPath = "Give path here";
                        object obj = HttpContext.GetLocalResourceObject(virtualPath, strResID + ".Text");
                        return obj != null ? obj.ToString() : "";
                }
    }
}

 

Thank you, jerryjoseph. I got to the documentation myself. What I was asking is how to get this virtual path since obviously I do not want to hard-code it. 

GetLocalResourceObject() was able to take care of picking the right resource file according to the local
 culture selected. So is there any way to keep this functionality?

ahmd01

What I was asking is how to get this virtual path since obviously I do not want to hard-code it. 

I don't understand your problem. Do you have multiple resource files? The path keeps changing?

OK, is virtualPath the path to the folder with resources, or to the actual resource file?

If it's a folder, then no, it doesn't change. Otherwise, if it needs a resource file, its name will be dependent on the culture currently selected. So that's what I'm asking.

Hi,

I agree with jerryjoseph. In my opinion,virtualPath is the path to the folder with resources and is a folder.

Mimicking existing page to recreate absolute style values for a different page. Is there a better way

<div class="container"> <div class="body">

Hello,

I have a Silverlight video background for an ASP.NET page's background.  In order to do this, I needed to use style top and left coordinates for all of the Web controls.

I was thinking on using the existing page layout (without Silverlight), and extract the style top and left and apply them to the Silverlight video background page.

Is there a better way?

Thank you.

williamj

</div> </div>

 

My solution was to use an IFRAME tag.

All Web controls are exactly where they should be, but I am having difficulties with post-backs.  (They do not occur.)

I will test further.

Thank you.

williamj

 

 

Instead of using iframes, you can host the SilverLight vidoa in the same page. If you know the width/height of the video, create a panel control and put the code for silverlight video in it. Style the rest of the page as is.

 

Thanks for your post.

My website uses globalization, with differing locations of Web controls for different languages.

williamj

 

Hi,

Forgive me if I am still not clear about what is your issue.  In my opinion, you can use CSS to realize your purpose. I give you a sample below: 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<style type="text/css">
        .HyperlinkCssClass img
        {
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink1" runat="server" CssClass="HyperlinkCssClass" ImageUrl="~/ImageConverter/images/google.png" >HyperLink</asp:HyperLink>
       
    </div>
    </form>
</body>
</html>

 

If I have anything misunderstood, please let me know. Could you explian the following sentence.

williamj7

I was thinking on using the existing page layout (without Silverlight),

Thank you for your help.

 

Hello,

I am using absolute values to position the Web controls in order to incorporate a Silverlight background.

Since I am using localized strings, the Web controls will not be positioned properly.  Hence, the use of an IFRAME.

williamj

CoolCryLaughing

 

Hi williamj7,  

In order to reslove your issue, could you please provide more information about your code?Thank you.

 

Hello,

I have a Silverlight MediaElement background with an IFRAME in the foreground.

I am also using div z-index style attributes to accomplish this.

Again, since the Website uses globalization, I think the best solution is to use an IFRAME, since using absolute (top and left) styles is too difficult to accomplish.  (for differing languages.)

Hope this helps.

williamj

 

Hi,

Forgive me if I am still not clear about what is your purpose.  In order to resolve your issue, could you provide a Screenshot? Thank you for your help.