Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Frank Gillebaard 8 posts 28 karma points
    Apr 14, 2011 @ 17:31
    Frank Gillebaard
    0

    RETRY Set text property in a form

    IM NOT SURE IF THE FIRST TRY IS SITLL ONLINE. BUT THIS IS THE RIGHT VERSION.

    Hello,

    And another question from me. We have a web service that has many functions. One of its functions is to keep in one place all multi-language string values. So I can’t make use of the Umbraco Dictionary functions. The reason is that we will build in the future other presentation channels (like native apple code and android code and so on) that will not have Umbraco at all. But we still want a single point where we keep the display texts for all presentation channels.

    The second point I want to make before I ask the question is. I like to use only Umbraco and Razor to make the code. So I do not want to make user controls (I think that’s old technology).

    I did build a c# library that gets all the multi-language strings for a form and saves the data in the current httpcontext (see code below)

            public static bool SetTextGroupInContext(string groupIdentifier, int language)

            {

                try

                {

                    MultiLanguageText[] languageItems = GetPublicTextResource(groupIdentifier, language);

                    foreach (MultiLanguageText languageItem in languageItems)

                    {

                        System.Web.HttpContext.Current.Items.Add(languageItem.TextIdent, languageItem.Text);

                    }

                    return true;

                }

                catch

                {

                    return false;

                }

            }

            private static MultiLanguageText[] GetPublicTextResource(string groupIdentifier, int language)

            {

                Spilter400ServiceClient client = new Spilter400ServiceClient();

                MultiLanguageText[] result = client.GetPublicTextResource(groupIdentifier, language);

                return result;

            }

     

    To use it i have a razor script that gets the data back into the form. This works fine except for controls. It turns out that you can not set the control property text with a razor script.

     

    So I made code that I call at the end of the form. This code loops through all controls and if it has the ID that I look for it will change the text property. (see code below). What I like to know is: Is there a more easy solution for this problem?

     

     

            private static void SetControlText(Control control, string controlID, string textIdentifier)

            {

                if (control.ID == controlID)

                {

                    if (control.GetType().ToString() == "System.Web.UI.WebControls.Button")

                    {

                        System.Web.UI.WebControls.Button currentButton = (System.Web.UI.WebControls.Button)control;

                        currentButton.Text = (string)System.Web.HttpContext.Current.Items[textIdentifier];

                    }

                    // future other control types

                }

            }

            public static void SetControlText(string controlID, string textIdentifier)

            {

                Page page = HttpContext.Current.Handler as Page;

                foreach (Control control in page.Form.Controls)

                {

                    SetControlText(control, controlID, textIdentifier);

                    SetControlText(control.Controls, controlID, textIdentifier);

                }

            }

     

            private static void SetControlText(ControlCollection nextControl, string controlID, string textIdentifier)

            {

                foreach (Control control in nextControl)

                {

                    SetControlText(control, controlID, textIdentifier);

                    SetControlText(control.Controls, controlID, textIdentifier);

                }

            }

     

    Met vriendelijke groet,

    Frank Gillebaard

Please Sign in or register to post replies

Write your reply to:

Draft