【问题标题】:Error when fetching data from database to autocomplete textbox从数据库获取数据到自动完成文本框时出错
【发布时间】:2014-09-25 07:00:18
【问题描述】:

我有一个表单,我正在使用一个自动完成文本框(使用 ajax 自动完成扩展器)。
自动完成功能正常工作。但是当我尝试从数据库中获取数据并尝试在我的表单中显示它时,值不会显示在表单上。

当我从页面评论 ajax 自动完成扩展程序时,所有值都会显示。为什么会发生这种情况?

我需要表单中的自动完成功能。

<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  
}

【问题讨论】:

  • 任何代码?我猜不出你是如何实现它的。
  • 什么时候调用getdata()?我的意思是在哪个事件上
  • 我的表单中另一个文本框的 textchanged 事件
  • 是否允许autopostback?这不是update panel中的一个吗?
  • 是的,它允许回发。整个表单都在更新面板内。

标签: c# asp.net


【解决方案1】:

Ajax 自动完成添加

OnClientItemSelected="mycustomers" OnClientShowing="clientShowing

     Call mycustomers() and clientShowing() in Javascript 

      Function mycustomers(){
      var str = $('#<%=txtname.ClientID %>').val();
       var partsOfStr = str.split(",");
        $.trim("partsOfStr");

      //spilt your string like this 
        $('#<%=txtname.ClientID %>').val(partsOfStr[0]);
        $('#<%=txtcontact.ClientID %>').val(partsOfStr[1]);


       }

       function clientShowing(source, args) {
        var popup = source._completionListElement;
        var height = popup.scrollHeight;
        var width = popup.scrollWidth;
        source._popupBehavior._element.style.height = "130px";
        source._popupBehavior._element.style.zIndex = 100000;
        //This iframe's height and width should be smaller than the CompletionList but bigger     than the DropDownList
        var iframe1 = "<IFRAME id='iframe1' style=' height:200px; width:10px; position:absolute; z-index:99999;border:0px; border-style:none; border-width:0px; ' ></IFRAME>";
        popup.innerHTML = iframe1 + popup.innerHTML;
    }

在你的 .cs 页面中调用一个 webservice 方法

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod (EnableSession=true)]


     public static List<string> SearchCustomers(string prefixText, int count)
     {
       sql = Select Name+ ',' + Contact+ ',' as Name From Table  where Name Like '%'+@Name+'%' 

       // Find the datatable or Dataset 

         // THEN add
        List<string> Contact= new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {

            Company.Add(dt.Rows[i]["Name"].ToString());

        }
        return Contact;
    }
      }

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多