locked
Bind to Text Property of Composite Control RRS feed

  • Question

  • User-1731144759 posted

    I have a composite control that consists of a TextBox and a RequiredFieldValidator, so it's basically a Required TextBox control.  The control is located inside of a formview, when in edit mode, and I want to Bind the database value to the Text property of the Text Box.  How do I do this?  Thanks!  Below is my code for reference

    [ToolboxData(@"<{0}:TextBoxReqd TextBoxID=""txt"" ValidatorID="""" Text="""" ValidationGroup="""" ValidatorText=""*"" ValidatorErrorMessage="""" runat=""server"" />")]
        public class TextBoxReqd : CompositeControl, INamingContainer
        {
            private string _textBoxID = "thisTextBox";
            private string _text = "";
            private double _textBoxWidth = 0;
            private int _textBoxMaxLength = 0;
    
            private string _rfvID = "thisRFV";
            private string _validationGroup = "";
            private string _validationText = "*";
            private string _validationErrorMessage = "";
    
            #region Properties
            public string TextBoxID
            {
                get { return _textBoxID; }
                set { _textBoxID = value; }
            }
    
            [Bindable(true)]
            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }
    
            public double TextBoxWidth
            {
                get { return _textBoxWidth; }
                set { _textBoxWidth = value; }
            }
    
            public int TextBoxMaxLength
            {
                get { return _textBoxMaxLength; }
                set { _textBoxMaxLength = value; }
            }
    
            public string ValidatorID
            {
                get { return _rfvID; }
                set { _rfvID = value; }
            }
    
            public string ValidationGroup
            {
                get { return _validationGroup; }
                set { _validationGroup = value; }
            }
    
            public string ValidatorText
            {
                get { return _validationText; }
                set { _validationText = value; }
            }
    
            public string ValidatorErrorMessage
            {
                get { return _validationErrorMessage; }
                set { _validationErrorMessage = value; }
            }
            #endregion Properties
    
            protected override void CreateChildControls()
            {
                TextBox txtReqd = new TextBox();
                txtReqd.ID = _textBoxID;
                txtReqd.Text = Text;
                if(TextBoxWidth > 0)
                    txtReqd.Width = new Unit(TextBoxWidth, UnitType.Em);
                txtReqd.MaxLength = TextBoxMaxLength;
    
                RequiredFieldValidator rfv = new RequiredFieldValidator();
                rfv.ID = _rfvID;
                rfv.ControlToValidate = _textBoxID;
                rfv.Display = ValidatorDisplay.Dynamic;
                rfv.Text = ValidatorText;
                rfv.ValidationGroup = ValidationGroup;
                rfv.ErrorMessage = ValidatorErrorMessage;
    
                Controls.Add(txtReqd);
                Controls.Add(rfv);
            }
        }


     

    And here is how I drop it on an .aspx page

    <sweCC:TextBoxReqd ID="TextBoxReqdJobCd" runat="server" Text='<%# Bind("JobCd") %>'
                                TextBoxID="txtJobCd" ValidationGroup="ValUpdCon" ValidatorErrorMessage="JobCd is required"
                                TextBoxMaxLength="50" ValidatorID="" ValidatorText="*" />


     

    Wednesday, October 14, 2009 7:12 PM

Answers

  • User191633014 posted

    try someting like this:


    public class TextBoxReqd : CompositeControl, INamingContainer
    {
        private TextBox txtReqd;

        [Bindable(true)]
        public string Text
        {
            get
            {
                this.EnsureChildControls();
                return txtReqd.Text;
            }
            set
            {
                this.EnsureChildControls();
                txtReqd.Text = value;
            }
        }
        ............

        protected override void CreateChildControls()
        {
            txtReqd = new TextBox();
            txtReqd.ID = _textBoxID;
         .............
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2009 8:32 AM