文本更改仅在回发后可见

时间:2012-12-03 10:01:23

标签: c# asp.net webpage lifecycle page-lifecycle

我有一个方法可以更改我网站上控件的文本 当用户加载页面时,这些更改应该是可见的 但是这些变化在下一次回发后首先可见。

我尝试在Page_PreInitPage_InitPage_PreLoad和所有其他描述的here方法中调用它。
但它们都没有奏效。

一些代码:
使用方法的类:(部分)

    namespace MyNamespace {
        public class ControlTextCorrection {

            Page _page;

            public ControlTextCorrection(Page page) {
                _page = page;
            }

            public void Correct() {
                HtmlEncodeControls(_page);
            }

            private void HtmlEncodeControls(Page page) {
                Control control = (Control)page;
                HtmlEncodeControls(control);
            }

            private void HtmlEncodeControls(Control parentControl) {
                if (!parentControl.HasControls()) {
                    return;
                }
                foreach (Control control in parentControl.Controls) {
                    if (control.HasControls()) {
                        HtmlEncodeControls(control);
                    }
                    if (control is Label) {
                        Label label = (Label)control;
                        label.Text = HtmlTextCorrection(label.Text);
                    }
                    else if (control is CheckBox) {
                        CheckBox checkBox = (CheckBox)control;
                        checkBox.Text = HtmlTextCorrection(checkBox.Text);
                    }
                    //Correction for more controls...
                }
            }

            protected string HtmlTextCorrection(string text) {
                bool encode = true;
                while (encode) {
                    string newText = _page.Server.HtmlDecode(text);
                    if (newText == text) {
                        encode = false;
                    }
                    text = newText;
                }
                text = _page.Server.HtmlEncode(text);
                return text;
            }
        }
    }  

调用方法的示例:

    protected void Page_PreLoad(object sender, EventArgs e) {
        ControlTextCorrection correction = new ControlTextCorrection(this.Page);
        correction.Correct();
    }

那么,在哪里(何时)我应该调用它以便在第一眼看到这些变化时可见?

0 个答案:

没有答案
相关问题