0

I have a custom override textbox with checkbox control. The control works fine unless I try to reference the Control's TextBox/CheckBox ID in Javascript then I get a HttpCompileException before it renders the control. Is there an override event that works better for adding multiple controls in one control, or a better approach to this completely?

Control on Page: OverridableTextBox runat="server" ID="otbRate" TextBoxID="txtRate" CheckBoxID="chbRate">

Referencing ID in JavasScript: "<%=txtRate.ClientID %>" causes HttpCompileException on LoadControl event for .ascx page

OverrideTextBox Class

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

1 Answer 1

0

Your page can't access txtRate textbox directly as it placed in Controls collection of the otbRate control. Try this instead: '<%= otbRate.ClientID + "_txtRate" %>'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.