views:

35

answers:

1

I'd like to localize my aspx-page.

This should include dynamically created LinkButtons in a GridView inside of InstantiateIN

(amendment 1: implementation of the System.Web.UI.ITemplate.InstantiateIN method to manipulate the appearance of a GridView)

(amendment 2: first six lines of code added to better indicate location of other code)

But inside InstantiateIN I cannot use (see) the method GetLocalResourceObject

Solution: use a Session-Variable

Question: Why can't I use GetLocalResourceObject inside InstantiateIN?

the following happens inside InstantiateIN

public class DynamicTemplateGridViewSearch : ITemplate
{
  public void InstantiateIn(System.Web.UI.Control Container)
  {
   switch (ItemType)
   {
    case ListItemType.Item:
    switch (InfoType)
    {
        case "Command":
            {
                LinkButton search_button = new LinkButton();
                search_button.ID = "search";
                search_button.CommandName = "Edit";
                //following line does not work. Error is:
                //The name 'GetLocalResourceObject' does not exist in the current context
                search_button.Text = GetLocalResourceObject("SearchButtonResource1.Text").ToString();
                //so I have to create a Session-String in Page_Load
                //which is referenced here
                search_button.Text = (string)new Page().Session["SearchText"]; // "Search";
                search_button.Click += new EventHandler(search_button_Click);
                Container.Controls.Add(search_button);
A: 

You haven't told where exactly InstantiateIn method is located - is it inside an custom control?

Regardless, you can use container.Page property to get reference to page object and invoke the method on it. For example,

search_button.Text = Container.Page.GetLocalResourceObject("SearchButtonResource1.Text").ToString();

BTW, session object reference can be obtained similarly or you may use current HttpContext. For example, Container.Page.Session["SearchText"] OR HttpContext.Current.Session["SearchText"]

VinayC
VinaiC, thanks for your reply. I've tried your suggestion, but GetLocalResourceObject does NOT appear in IntelliSense as a method (for some reason there is no visibility from this place). For better comprehension I've added six leading code lines. It is related to the implementation of the System.Web.UI.ITemplate.InstantiateIN method to manipulate the appearance of a GridView. We add programmatically a LinkButton (and lots of other stuff) and would like to localize all those texts.
Henry99
@Henry99, can you see Page property for Container in intellisense? Anyway, Container is control and it will have Page property to get the page object and Page has GetLocalResourceObject method. You should try compiling that code and check if you get any errors.
VinayC
@VianyC, thanks alot for your time spent! (my answer is split in various comments) I write .NET Business-Applications and am not THE code guru concerning the understanding of ASP.NET server controls. I use standard coding to make appear controls inside of an ASP.NET GridView. For example a LinkButton which Text-Attribute is “Search” that should be localized. For this I create a Class DynamicTemplateGridView : ITemplate (see code above) Originally I’ve tried the following to localize strings (inside the InstantiateIN method of DynamicTemplateGridView)
Henry99
1. string value = Page.GetLocalResourceObject("lnkBtnSearchButton.Resource1.Text").ToString();Error to 1.: 'System.Web.UI.TemplateControl.GetLocalResourceObject(string, System.Type, string)' is inaccessible due to its protection levelSo the workaround is to get the value “lnkBtnSearchButton.Resource1.Text” localized from INSIDE InstantiateIN, I´ve put it into a Session variable and use the following very strange syntax to view it inside the InstantianteIN:(NO error here, it works!)string value = new Page().Session["lnkBtnSearchButton.Resource1.Text"].ToString();
Henry99
What does NOT work is: 2. string value = Session["lnkBtnSearchButton.Resource1.Text"].ToString(); Error to 2.: The name 'Session' does not exist in the current context
Henry99
3. string value = Page.Session["lnkBtnSearchButton.Resource1.Text"].ToString(); Error to 3. An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'
Henry99
Understood the issue - for #2/3, try Container.Page.Session["SearchText"] or HttpContext.Current.Session["SearchText"].
VinayC
Container.Page.Session["SearchText"] returns NULL
Henry99
HttpContext.Current.Session["SearchText"] does work, thanks and probably will perform faster than instantiate new Page().Session["SearchText"]
Henry99