在列表框中列出搜索结果

时间:2013-08-08 00:28:58

标签: asp.net vb.net search listbox telerik

对于新的管理员。我的应用程序的功能,有一个GridView来显示数据库中的记录。

TextBox让用户输入员工姓名,然后点击“搜索”按钮。我想在ListBox中显示搜索结果(显示类似的员工姓名)。当用户单击该名称时,它将自动插入到db中,然后刷新GridView

如何基于ListBoxTextBox进行数据绑定?如何在用户点击ListBox时插入值?感谢。

<%@ 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>

<body>
  <form id="form1" runat="server">
    <div style="width: 500px; margin-left: auto; margin-right: auto;">
      <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
      </telerik:RadScriptManager>
      <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;"
                       OnDeleteCommand="RadGrid1_DeleteCommand">
        <ClientSettings AllowColumnsReorder="True" 
                        ReorderColumnsOnClient="True">
          <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <MasterTableView DataSourceID="LDS" DataKeyNames="ID">
          <CommandItemSettings ExportToPdfText="Export to Pdf">
          </CommandItemSettings>
          <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
          </RowIndicatorColumn>
          <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
          </ExpandCollapseColumn>
          <Columns>
            <telerik:GridBoundColumn DataField="DisplayName"
                                     FilterControlAltText="Filter Name column"
                                     HeaderText="Staff Name" ReadOnly="True"
                                     SortExpression="DisplayName"
                                     UniqueName="DisplayName">
            </telerik:GridBoundColumn>                
            <telerik:GridButtonColumn ConfirmText="Delete this record?"
                                      ConfirmDialogType="RadWindow"
                                      ConfirmTitle="Delete" 
                                      ButtonType="ImageButton" Text="Delete"
                                      CommandName="Delete" />
          </Columns>
          <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
          </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
        </HeaderContextMenu>
      </telerik:RadGrid>
      <asp:LinqDataSource ID="LDS" runat="server" 
                          ContextTypeName="DataContext"
                          OrderBy="DisplayName" Select="new (ID, DisplayName)"
                          TableName="v_EmailAlerts" Where="Type == 4">
        <WhereParameters>
          <asp:QueryStringParameter Name="CID" QueryStringField="CID"
                                    Type="Int32" />
        </WhereParameters>       
      </asp:LinqDataSource>
      <br />
      <br />
      <asp:TextBox runat="server" ID="tb_staffname">
      </asp:TextBox>
      <asp:Button ID="btnSearch" runat="server" Text="Search"
                  OnClick="btnSearch_click"/>
      <telerik:RadListBox runat="server" ID="RadListBox_staff" Height="300px"
                          Width="230px" AllowTransfer="false" 
                          AutoPostBack="false" 
                          style="top: 0px; left: 0px" 
                          DataSourceID="LDS_staff"
                          DataTextField="displayname" DataValueField="sid" >
      </telerik:RadListBox>
      <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">   
      </asp:LinqDataSource>                
   </div>
 </form>
</body>
</html>

Code Behind(使用建议的listbox数据绑定功能):

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 a.id = AlertID).firstordefault

        If rec IsNot Nothing Then
            dc.HRIS_EmailAlerts.DeleteOnSubmit(rec)
            dc.SubmitChanges()
        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
            RadListBox_staff.DataBind()
        End If
    End Sub
End Class

此外,它显示搜索功能的LINQ

的错误
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

1 个答案:

答案 0 :(得分:0)

让事情变得更简单......将您的编辑表单移动到具有如下条目的用户控件:

<EditFormSettings EditFormType="WebUserControl" UserControlName="adminLookup.ascx">

adminLookup.ascx中添加绑定到您希望能够查找的用户列表的RadSearchBox。然后,您应该能够在隔离的用户控件中轻松调试和解决查询查询的任何问题。

此功能的演示可在此处找到: http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/linqdatasource/defaultcs.aspx