チェックボックスコントロールを備えたカスタムオーバーライドテキストボックスがあります。コントロールの TextBox/CheckBox ID を Javascript で参照しようとしない限り、コントロールは正常に動作し、コントロールをレンダリングする前に HttpCompileException を取得します。複数のコントロールを 1 つのコントロールに追加するのに適したオーバーライド イベントはありますか、それとも完全にこれに対するより良いアプローチですか?
ページ上のコントロール: OverridableTextBox runat="server" ID="otbRate" TextBoxID="txtRate" CheckBoxID="chbRate">
JavaScript で ID を参照する: "<%=txtRate.ClientID %>" により、.ascx ページの LoadControl イベントで HttpCompileException が発生する
OverrideTextBox クラス
private string _TextBoxID = "";
private string _CheckBoxID = "";
private bool _AlignOverrideLeft = false;
private string _CheckBoxText = "";
public string TextBoxID
{
get { return _TextBoxID; }
set { _TextBoxID = value; }
}
public string CheckBoxID
{
get { return _CheckBoxID; }
set { _CheckBoxID = value; }
}
public string CheckBoxText
{
get { return _CheckBoxText; }
set { _CheckBoxText = value; }
}
public bool AlignOverrideLeft
{
get { return _AlignOverrideLeft; }
set { _AlignOverrideLeft = value; }
}
public TextBox TextBox = new TextBox();
public CheckBox CheckBox = new CheckBox();
protected override void OnInit(EventArgs e)
{
Table tbl = new Table();
TableRow tr = new TableRow();
TableCell tdOne = new TableCell();
TableCell tdTwo = new TableCell();
if (AlignOverrideLeft)
{
tdOne.Controls.Add(CheckBox);
tdTwo.Controls.Add(TextBox);
}
else
{
tdOne.Controls.Add(TextBox);
tdTwo.Controls.Add(CheckBox);
}
if (_TextBoxID != "") { TextBox.ID = _TextBoxID; }
if (_CheckBoxID != "") { CheckBox.ID = _CheckBoxID; }
CheckBox.Text = _CheckBoxText;
tr.Cells.Add(tdOne);
tr.Cells.Add(tdTwo);
tbl.Rows.Add(tr);
this.Controls.Add(tbl);
}
protected override void Render(HtmlTextWriter w)
{
if (CheckBox.Checked)
{
TextBox.ReadOnly = false;
}
else
{
TextBox.ReadOnly = true;
}
string CheckBoxClickJS = "var chb = d.getElementById('" + CheckBox.ClientID + "'); var txt = d.getElementById('" + TextBox.ClientID + "');";
CheckBoxClickJS += "if (chb.checked) { EnableTextBox(txt); txt.select(); } else { DisableTextBox(txt); }";
if (CheckBox.Attributes["onclick"] != null)
{
CheckBox.Attributes.Add("onclick", CheckBox.Attributes["onclick"].ToString() + CheckBoxClickJS);
}
else
{
CheckBox.Attributes.Add("onclick", CheckBoxClickJS);
}
base.Render(w);
}