来自数据库的 Ajax 自动完成扩展器

发布于 2024-09-07 17:46:02 字数 2501 浏览 10 评论 0原文

我想在我的 aspx 应用程序中创建一个文本框(用于输入姓名),该文本框建议数据库中的员工姓名。

我将我的代码粘贴在下面。我使用的是Mysql

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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>Untitled Page</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
       <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
                  ServiceMethod="GetCompletionList2" TargetControlID="TextBox1" 
                  UseContextKey="True">
       </asp:AutoCompleteExtender>
    </div>  
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    </form>
  </body>
</html>

Default.aspx.cs:

// here we use a readymade class for database operations to Mysql server
using System;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using Gbs.DAL;  // for ..redymade class..

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList2(string prefixText, int count, string contextKey)
    {
        myDb db = new myDb();// its class in Dal.cs

        string sql = "Select * from empdata Where empname like @prefixText";
        DataTable dt = db.getDataTable(sql);     // method in redymade class
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["empname"].ToString(), i);
            i++;
        }
        return items;
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    }
}

// 没有任何错误,程序运行成功

I want to create a textbox (for entering names) in my aspx application which suggests names of Employees from database.

I am pasting my code below. I am using Mysql

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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>Untitled Page</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
       <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
                  ServiceMethod="GetCompletionList2" TargetControlID="TextBox1" 
                  UseContextKey="True">
       </asp:AutoCompleteExtender>
    </div>  
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    </form>
  </body>
</html>

Default.aspx.cs:

// here we use a readymade class for database operations to Mysql server
using System;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using Gbs.DAL;  // for ..redymade class..

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList2(string prefixText, int count, string contextKey)
    {
        myDb db = new myDb();// its class in Dal.cs

        string sql = "Select * from empdata Where empname like @prefixText";
        DataTable dt = db.getDataTable(sql);     // method in redymade class
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["empname"].ToString(), i);
            i++;
        }
        return items;
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    }
}

// there is no any error, the program runs successfully

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2024-09-14 17:46:02

AutocompleteExtender 需要一个可以从 javascript 调用的 .asmx Web 服务。
它在您的 Default.aspx 中不起作用。

AutocompleteExtender needs an .asmx web service which can be called from javascript.
It won't work from your Default.aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文