(URGENT) Extending RadSearchBox to get Value field 2-way bound

2 posts, 0 answers
  1. Infodom
    Infodom avatar
    1 posts
    Member since:
    Aug 2013

    Posted 29 Nov 2013 Link to this post

    Hi all,

    I need to extend RadSearchBox because this concept with only Text value (that is not persisted) now is unusable.

    Lets say I have a case when I need to set a City to a Person. I have foreign key of the City in the Person entity and I am editing it. Lets also say there are 5 cities of the same name so I need to set VALUE (eg. foreign key) and text is pretty much unusable at this point. I cannot have preset city (if I am editing existing Person that moved to another city) and also I can only get the value after the user selected it (in event) so I cannot have 2 way binding here because VALUE field is only available in Search event.

    So my question here is how do I extend RadSearchBox to have VALUE field in 2 way binding scenario? I tried to extend RadSearchBox with 2 hidden fields and overried OnSearch event to store Text and Value in those fields but OnSearch is not overridable so I need some other way to make Value and Text fields persistent or use some other control (please advise which).

    Here is my code that I tried which obviously does not work:

    public class RadSearchBoxExtender : RadSearchBox, INamingContainer
    {
        #region Public properties.
     
        public string Value
        {
            get
            {
                this.EnsureChildControls();
                return this.valueField.Value;
            }
            set
            {
                this.EnsureChildControls();
                this.valueField.Value = value;
            }
        }
     
        public override string Text
        {
            get
            {
                this.EnsureChildControls();
                return this.textField.Value;
            }
            set
            {
                this.EnsureChildControls();
                this.textField.Value = value;
                base.Text = value;
            }
        }
     
        #endregion Public properties.
     
        #region Private methods.
     
        private HiddenField valueField;
        private HiddenField textField;
     
        private string valueFieldId
        {
            get
            {
                return this.ID + "_Value";
            }
        }
     
        private string textFieldId
        {
            get
            {
                return this.ID + "_Text";
            }
        }
     
        #endregion Private methods.
     
        #region Overrides.
     
        protected override void CreateChildControls()
        {
            this.valueField = new HiddenField();
            this.valueField.ID = this.valueFieldId;
            this.valueField.Value = null;
     
            this.textField = new HiddenField();
            this.textField.ID = this.valueFieldId;
            this.textField.Value = null;
     
            this.Controls.Add(this.valueField);
            this.Controls.Add(this.textField);
     
            base.CreateChildControls();
        }
     
        #endregion Overrides.
     
        protected new void OnSearch(SearchBoxEventArgs e)
        {
            base.OnSearch(e);
     
            this.EnsureChildControls();
            this.valueField.Value = e.Value;
            this.textField.Value = e.Text;
        }
     
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
     
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
     
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            this.valueField.RenderControl(writer);
        }
    }
  2. Plamen
    Admin
    Plamen avatar
    2729 posts

    Posted 04 Dec 2013 Link to this post

    Hello,

    I believe I need to provide a little bit more information about RadSearchbox as a whole before explaining the issue. By design RadSearchBox for ASP.NET AJAX is a light control which provides the ability to write text in an input field with an optional autocomplete functionality or searching the plain result of the text written in it. That is why it does not have real item collection which can allow setting value as in the scenario you are trying to implement. In such cases we kindly recommend using some other of our controls that provide items collection and the ability to bind it with different properties as RadComboBox and RadAutoCompleteBox. Unfortunately RadSearchbox is not designed to work in such scenarios. Please excuse us for this limitation of our control.


    Regards,
    Plamen
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top