List search result in Listbox

2 posts, 0 answers
  1. Joe
    Joe avatar
    57 posts
    Member since:
    Mar 2013

    Posted 07 Aug 2013 Link to this post

    for the new admin. function of my application, there is a gridview to show records in database.

    there is a textbox to let user input staff name and then click "search" button. I want to show the search result in Listbox (show similar staff name). when user click on the name, it will automatically insert into db and then refresh the gridview.

    How can I databind the listbox based on the textbox ? and how can i insert the value when user click listbox ? Thanks.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="ITAlert.aspx.vb" Inherits="DepartmentManager_ITAlert" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <head runat="server">
        <form id="form1" runat="server">
        <div style="width: 500px; margin-left: auto; margin-right: auto;">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
                        <asp:Label ID="Label1" runat="server" Text="Staffs to get the email alert:" Font-Bold="true"></asp:Label>
                        <br /><br /> 
                        <telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True" allowpaging="true"
                        AutoGenerateColumns="False" DataSourceID="LDS" GridLines="None"  pagesize="20"       
                        AllowAutomaticDeletes="True" Style="border: 0; outline: none;"
                        <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                            <Selecting AllowRowSelect="True" />
                        <MasterTableView DataSourceID="LDS" DataKeyNames="ID">
                            <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                <HeaderStyle Width="20px"></HeaderStyle>
                            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                <HeaderStyle Width="20px"></HeaderStyle>
                                <telerik:GridBoundColumn DataField="DisplayName" FilterControlAltText="Filter Name column"
                                    HeaderText="Staff Name" ReadOnly="True" SortExpression="DisplayName" UniqueName="DisplayName">
                            <telerik:GridButtonColumn ConfirmText="Delete this record?" ConfirmDialogType="RadWindow"
                              ConfirmTitle="Delete" ButtonType="ImageButton" Text="Delete" CommandName="Delete" />
                                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                        <FilterMenu EnableImageSprites="False">
                        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                    <asp:LinqDataSource ID="LDS" runat="server" ContextTypeName="DataContext"
                        OrderBy="DisplayName" Select="new (ID, DisplayName)" TableName="v_EmailAlerts"
                        Where="Type == 4">
                                        <asp:QueryStringParameter Name="CID" QueryStringField="CID"
                                            Type="Int32" />
                    <br />
                    <br />
                    <asp:TextBox runat="server" ID="tb_staffname">
                    <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_click"/>
               <telerik:RadListBox runat="server" ID="RadListBox_staff" Height="300px" Width="230px"
                style="top: 0px; left: 0px" DataSourceID="LDS_staff"
                DataTextField="displayname" DataValueField="sid" >
                                  <asp:LinqDataSource ID="LDS_staff" runat="server" ContextTypeName="dcHRISDataContext"
                        OrderBy="DisplayName" Select="new (SID, DisplayName)" TableName="vHRIS_StaffDBs"
                        Where="Lefe == False and SID is nothing">  

    Code Behind (with proposed listbox databind function) :

    Imports Telerik.Web.UI
    Partial Class DepartmentManager_ITAlert
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub
        Protected Sub RadGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.DeleteCommand
            Dim AlertID = DirectCast((DirectCast(e.Item, GridDataItem)).GetDataKeyValue("ID"), Integer)
            'retrive entity form the Db
            Dim dc As New DataContext
            Dim rec = (From a In dc.EmailAlerts Where = AlertID).firstordefault
            If rec IsNot Nothing Then
            End If
        End Sub
        Protected Sub btnSearch_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
            Dim sname As String = ""
            sname = tb_staffname.text
            If sname <> "" Then
                Dim dc As New dcHRISDataContext
                Dim rec = (From a In dc.vHRIS_StaffDBs Where a.Lefe = False _
                           And (x >= x.DisplayName.Matches(sname)) Select a.SID, a.DisplayName Order By DisplayName).ToList
                RadListBox_staff.DataTextField = "DisplayName"
                RadListBox_staff.DataValueField = "SID"
                RadListBox_staff.DataSource = dt
            End If
        End Sub
    End Class

    moreover, it display error for the linq of searching function

    Dim rec = (From a In dc.vHRIS_StaffDBs Where a.Lefe = False _
               And (x >= x.DisplayName.Matches(sname)) Select a.SID, a.DisplayName Order By DisplayName).ToList

    the error msg as follows:

    Compiler Error Message: BC36610: Name 'x' is either not declared or not in the current scope.

  2. Kate
    Kate avatar
    1898 posts

    Posted 09 Aug 2013 Link to this post

    Hello Joe,

    Please try using the RadSearchBox control instead of a simple input. Here you can check our over view demo and the server-side events that you can use with the control. I would also suggest that you review all of the demos to get a better understanding of the possible scenarios where the control can be applied.

    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.
Back to Top