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

[Solved] search in list box

1 Answer 164 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Islam Abdel Aziz
Top achievements
Rank 1
Islam Abdel Aziz asked on 28 Jun 2010, 10:45 AM
Hello ,

I have a list box which items are check boxes
I need to add a textbox that filters that list items by the check boxes content
Is there any telerik control that perform this function?

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 30 Jun 2010, 01:01 PM
Hello Islam,

For your convenience I prepared a sample case which represents how to filter items in RadListBox (with checkboxes enabled) on each key press when typing inside RadTextBox. The operation is performed via ajax request using RadAjaxManager instance.

Examine the code snippets posted below for further details (note that I used the well-known Northwind SQL database for source):

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
 <script type="text/javascript">
     var timer = null; 
     function KeyUp() {
         if (timer != null) {
             clearTimeout(timer);
         }
         timer = setTimeout(LoadContent, 500);
     }
     function LoadContent() {
         $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("FilterListBox");
     }  
 </script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager id="RadAjaxManager1" runat="server" 
    onajaxrequest="RadAjaxManager1_AjaxRequest" >
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadListBox1" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default" />
<telerik:RadTextBox ID="RadTextBox1" runat="server" onkeyup="KeyUp();" />
<telerik:RadListBox ID="RadListBox1" runat="server" CheckBoxes="true" Width="200px"
    Height="300px">
</telerik:RadListBox>

protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
    if (e.Argument == "FilterListBox")
    {
        RadListBox1.DataSource = GetDataTable("SELECT ProductName FROM [Products] Where ProductName LIKE '" + RadTextBox1.Text + "%'");
        RadListBox1.DataBind();
    }
}
private DataTable GetDataTable(String queryString)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlConnection MySqlConnection = new SqlConnection(ConnString);
    SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
    MySqlDataAdapter.SelectCommand = new SqlCommand(queryString, MySqlConnection);
    DataTable myDataTable = new DataTable();
    MySqlConnection.Open();
    try
    {
        MySqlDataAdapter.Fill(myDataTable);
    }
    finally
    {
        MySqlConnection.Close();
    }
    return myDataTable;
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RadListBox1.DataTextField = "ProductName";
        RadListBox1.DataValueField = "ProductName";
        RadListBox1.DataSource = GetDataTable("SELECT ProductName FROM Products");
        RadListBox1.DataBind();
    }
}

Greetings,
Sebastian
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Islam Abdel Aziz
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or