2012-08-27 19 views
0

我有一個複選框控件的自定義覆蓋文本框。控制工作正常,除非我試圖引用控制的JavaScript中的TextBox/CheckBox ID,然後我得到一個HttpCompileException之前呈現該控件。是否有一個覆蓋事件更適合在一個控件中添加多個控件,或者更好地完成此操作?LoadControl HttpCompileException自定義服務器控件JavaScript錯誤

控制上頁: OverridableTextBox RUNAT = 「服務器」 ID = 「otbRate」 TextBoxID = 「txtRate」 CheckBoxID = 「chbRate」>中使用JavasScript

引用ID: 「<%= txtRate.ClientID%>」導致HttpCompileException上LoadControl事件的.ascx頁面

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); 
    } 

回答

0

你的頁面,因爲它放置在Controls集合無法訪問txtRate文本框直接otbRate控制。試試這個:'<%= otbRate.ClientID + "_txtRate" %>'

相關問題