从数据库中获取数据到自动完成文本框时出现问题 [英] Problem when fetching data from database to autocomplete textbox

查看:57
本文介绍了从数据库中获取数据到自动完成文本框时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我正在使用一个自动完成文本框(使用ajax自动完成扩展程序)。自动完成功能正常工作。但是当我尝试从数据库中获取数据并尝试在我的表单中显示它时(比如编辑一个记录)值不会显示在窗体上。只要我从页面注释ajax autocomplete extender,所有值都会显示。为什么会发生这种情况?我需要在我的表单中使用自动完成功能。帮助我。



 <   asp:TextBox     ID   =  txtContactsSearch    runat   =  server    autopostback   =  True >  <   / asp:TextBox  >  
< cc1: AutoCompleteExtender ServiceMethod = SearchCustomers

MinimumPrefixLength = 2

CompletionInterval = 100 EnableCaching = false CompletionSetCount = 10

TargetControlID = txtContactsSearch

ID = AutoCompleteExtender1 runat = server FirstRowSelected = false < span class =code-keyword>>
< / cc1:AutoCompleteExtender >

public void getdata()
{
Datatable dt = objdal.getdata();
Datarow dr = dt = .Rows [0];
txtContactsSearch.Text = dr [contact]。Tostring();
//表单上剩余文本框的空白代码
}

解决方案

搜索:<   asp:TextBox    < span class =code-attribute> ID   =  TextBox1    runat   = 服务器 >  <   / asp:TextBox  >  

< asp:AutoCompleteExtender ID = AutoCompleteExtender1 runat = 服务器

< span class =code-attribute> ServiceMethod = AutoCompleteAjaxRequest

< span class =code-attribute> ServicePath = AutoComplete.asmx

MinimumPrefixLength = 2

CompletionInterval = 100

< span class =code-attribute> EnableCaching = false

< span class =code-attribute> CompletionSetCount = 10

TargetControlID = TextBox1

< span class =code-attribute> FirstRowSelected = false >
< / asp:AutoCompleteExtender >



 上面的代码中你可以看到我已经指定了TargetControlID =   TextBox1。此标记将使用 指定中的,其中将显示控件自动建议。 

现在我们将看到hw
数据库接收数据对于 我们的AutoCompleteExtender标记,我们有两个属性ServiceMethod ServicePath。 中,我们将定义Web服务名称方法。 添加 webservice file 项目中的class =code-keyword>(.asmx)。所以这里 你的webservice的代码







使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;使用System.Web.Services
;
使用System.Data;
使用System.Data.OleDb;

名称空间ProjectDemo_Asp.et
{
/// < 摘要 >
///自动完成的摘要说明
/// < ; / summary >
[WebService(Namespace =http:/ /tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//允许调用此Web服务从脚本,使用ASP.NET AJAX,取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class AutoComplete:System.Web.Services.WebService
{

[WebMethod]
public string [] AutoCompleteAjaxRequest(string prefixText,int count)
{
List < string > ajaxDataCollection = new List < string > ();
DataTable _objdt = new DataTable();
_objdt = GetDataFromDataBase(prefixText);
if(_objdt.Rows.Count> 0)
{
for(int i = 0; i < _ objdt.Rows.Count; i ++)

{

ajaxDataCollection.Add(_objdt.Rows [i] [LanguageName]。ToString());

< span class =code-attribute> }

< span class =code-attribute> }

return ajaxDataCollection.ToArray();

}

/// < summary >
///从数据库中检索数据的功能
/// < / summary >
/// < 返回 > < / returns >
public DataTable GetDataFromDataBase(string prefixText)
{
string connectionstring =Provider = Microsoft.ACE.OLEDB.12.0; Data Source = | DataDirectory | \\ bookstore.mdb; Persist Security Info = False;;
DataTable _objdt = new DataTable();
string querystring =select * from ProLanguage,其中LanguageName如'%+ prefixText +%';;
OleDbConnection _objcon = new OleDbConnection(connectionstring);
OleDbDataAdapter _objda = new OleDbDataAdapter(querystring,_objcon);
_objcon.Open();
_objda.Fill(_objdt);
return _objdt;
}
}
}







读到这个你可以更清晰



http://www.dotnetfox.com/articles/ajax-autocomplete-example-with-database-in-Asp-Net-1079.aspx [ ^ ]


I have a form where i am using one autocomplete text box(using ajax autocomplete extender).Autocomplete functionality is working properly.But When I try to fetch data from database and try to display it in my form (say for editing a record) values doesnt display on form.As soon as i comment ajax autocomplete extender from page,all values gets displayed.Why is this happening? I need that autocomplete functionality in my form.Help Me.

<asp:TextBox ID="txtContactsSearch" runat="server" autopostback="True"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"

MinimumPrefixLength="2"

CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"

TargetControlID="txtContactsSearch"

ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>

public void getdata()
{
 Datatable dt=objdal.getdata();
 Datarow dr=dt=.Rows[0];
 txtContactsSearch.Text=dr["contact"].Tostring();
  //sililar code for remaining textboxes on form
}

解决方案

Search : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

      <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"

                  ServiceMethod="AutoCompleteAjaxRequest"

                  ServicePath="AutoComplete.asmx"

                  MinimumPrefixLength="2"

                  CompletionInterval="100"

                  EnableCaching="false"

                  CompletionSetCount="10"

                  TargetControlID="TextBox1"

                  FirstRowSelected="false">
      </asp:AutoCompleteExtender>


In above code you can see that I have specified the TargetControlID="TextBox1" . This tag will used for specifying that in which control auto suggest will appear.

Now we will see hw to receive data from data base. For this in our AutoCompleteExtender tag we have two properties ServiceMethod and ServicePath . In this we will define web service name and method . For this add a webservice file in your project (.asmx). So here is the code of you webservice.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;

namespace ProjectDemo_Asp.et
{
    /// <summary>
    /// Summary description for AutoComplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class AutoComplete : System.Web.Services.WebService
    {

        [WebMethod]
        public string[] AutoCompleteAjaxRequest(string prefixText, int count)
        {
            List<string> ajaxDataCollection = new List<string>();
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase(prefixText);
                if(_objdt.Rows.Count>0)
                {
                    for (int i = 0; i < _objdt.Rows.Count; i++)

                    {

                        ajaxDataCollection.Add(_objdt.Rows[i]["LanguageName"].ToString());

                    }

                }

            return ajaxDataCollection.ToArray();

        }

        /// <summary>
        /// Function for retriving data from database
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase(string prefixText)
        {
            string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
            DataTable _objdt = new DataTable();
            string querystring = "select * from ProLanguage where LanguageName like '%" + prefixText + "%';";
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
    }
}




read this you may get more clearity

http://www.dotnetfox.com/articles/ajax-autocomplete-example-with-database-in-Asp-Net-1079.aspx[^]


这篇关于从数据库中获取数据到自动完成文本框时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆