サイトのコントロールのテキストを変更する方法があります。
これらの変更は、ユーザーがページを読み込んだときにすぐに表示されるはずです。
ただし、変更は次のポストバック後に初めて表示されます。
Page_PreInit
、Page_Init
、およびここでPage_PreLoad
説明されている他のすべてのメソッドでそれを呼び出そうとしました。
しかし、どれも機能しませんでした。
いくつかのコード:
メソッドを持つクラス: (部分的に)
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();
}
では、サイトの一目で変更が見えるようにするには、いつどこで呼び出す必要がありますか?