This is a migrated thread and some comments may be shown as answers.

RadSearchBox with RadAjaxManager

1 Answer 91 Views
SearchBox
This is a migrated thread and some comments may be shown as answers.
Iago
Top achievements
Rank 1
Iago asked on 03 Jan 2018, 01:31 PM

Hello,

I have a RadSearchBox and a HiddenField. When user selects an item, my HiddenField gets its value from item's ID. But I would like both client and server side HiddenField's value become empty when text in RadSearchBox does not correspond to the HiddenField's value.

So I tried using RadAjaxManager:

01.<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdatePanelsRenderMode="Inline">
02.      <AjaxSettings>
03.          <telerik:AjaxSetting AjaxControlID="RadSearchBoxIns" EventName="DataSourceSelect">
04.              <UpdatedControls>
05. 
06.                  <telerik:AjaxUpdatedControl ControlID="HiddenFieldIns" />
07.                  <telerik:AjaxUpdatedControl ControlID="CustomValidatorRadSearchBoxIns" />
08.              </UpdatedControls>
09. 
10.          </telerik:AjaxSetting>
11.           <telerik:AjaxSetting AjaxControlID="RadSearchBoxIns" EventName="Search">
12.              <UpdatedControls>   
13.                   <telerik:AjaxUpdatedControl ControlID="HiddenFieldIns" />               
14.                  <telerik:AjaxUpdatedControl ControlID="CustomValidatorRadSearchBoxIns" />
15.              </UpdatedControls>
16. 
17.          </telerik:AjaxSetting>
18.      </AjaxSettings>
19.  </telerik:RadAjaxManager>

 

In both server events (  OnSearch and OnDataSourceSelect ) I set HiddenFieldIns value to the right ID or empty, respectively, but HiddenFieldIns remains the last selected value, even if user starts typing something else. In debug mode, I can see OnDataSourceSelect is hit and so, HiddenFieldIns's value is, apparently, set to an empty string. Anyway, DOM structure is not changed and when during validation this HiddenFieldIns's value is read again, it's back to its last selected value.

Could you help me to find a way to obtain the desired result?

 

Thank you in advance.

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 08 Jan 2018, 10:14 AM
Hello Iago,

The searching mechanism of the SearchBox uses Callbacks instead of partial postbacks and that is the reason for the HiddenField not to be updated when searching. Unfortunately, there is no public property that would allow subscribing to the event where the response is received from the server. Nevertheless, the following code could be used as a workaround and allow you to update the hidden field value when the response is received.

var original_onRequestEnd = Telerik.Web.UI.RadSearchBox.prototype._onRequestEnd
Telerik.Web.UI.RadSearchBox.prototype._onRequestEnd = function () {
    original_onRequestEnd.call(this);
    var searchbox = this;
    // custom code here to update the hidden field
}

Regarding the OnSearch event, it should be updating the hidden field value as demonstrated in this screencast.

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadSearchBox5" >
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadSearchBox5" />
                <telerik:AjaxUpdatedControl ControlID="HiddenField1" />
            </UpdatedControls>
        </telerik:AjaxSetting>             
    </AjaxSettings>
</telerik:RadAjaxManager>
<asp:HiddenField runat="server" ID="HiddenField1" Value="InitialValue" />
<telerik:RadSearchBox RenderMode="Lightweight" runat="server" ID="RadSearchBox5" EmptyMessage="XML"
    DataSourceID="XmlDataSource1" DataValueField="Value"           
    OnSearch="RadSearchBox5_Search"
    OnDataSourceSelect="RadSearchBox5_DataSourceSelect"
    DataTextField="Text" Width="300">
    <DropDownSettings Height="400" Width="300" />
</telerik:RadSearchBox>

protected void RadSearchBox5_Search(object sender, SearchBoxEventArgs e)
{
    HiddenField1.Value = e.Text;
}
 
protected void RadSearchBox5_DataSourceSelect(object sender, SearchBoxDataSourceSelectEventArgs e)
{
 
}

Regards,
Peter Milchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
SearchBox
Asked by
Iago
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Share this question
or