将AutoCompleteExtender动态绑定到Asp.Net中gridview的文本框

时间:2016-12-13 10:55:39

标签: c# asp.net autocompleteextender

我曾尝试在网格视图行数据绑定事件中从代码后面添加自动完成扩展程序,但是它给出了自动完成扩展程序未找到“TargetControlId”错误或自动完成扩展程序的重复控制。

我在下面的行中添加了.aspx和.cs文件的代码。

在.Aspx文件中

<asp:GridView ID="grd1" runat="server" Width="100%" AutoGenerateColumns="False"
                            OnRowDataBound="grd1_OnRowDataBound" OnSorting="grdDocket_OnSorting"
                            GridLines="None" OnDataBound="grd1_OnDataBound" ShowHeader="true" AllowSorting="true"
                            AllowPaging="false" PageSize="20" OnPageIndexChanging="grd1_OnPageIndexChanging"
                            PagerStyle-HorizontalAlign="Center">
     <Columns>
       <asp:TemplateField HeaderText="Judges" SortExpression="Judges">
        <HeaderStyle Width="5%" HorizontalAlign="Left" />
        <ItemStyle Width="5%" HorizontalAlign="Left" />
        <HeaderTemplate>
          XYZ  
        </HeaderTemplate>
       <ItemTemplate>
         <asp:TextBox ID="txtXyz" SkinID="txtUpperCase" runat="server" Width="32px" MaxLength="60" Text='<%# Eval("Xyz") %>' />
       </ItemTemplate>
     </asp:TemplateField>
   </Columns>
 </asp:GridView>

在.cs文件中

protected void grdDocket_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                switch (e.Row.RowType)
                {
                    case DataControlRowType.DataRow:
                        TextBox txtXyz = (TextBox)e.Row.FindControl("txtXyz");
                        var aceXyz = getAutoCompleteExtender("Xyz", txtXyz.ID);
                        e.Row.Controls.Add(aceJudge);
                }
            }
            catch (Exception ex)
            {
                TblSyExceptions.Log(ex, HttpContext.Current.ToString(), SessionWrapper.UserId);
                //this.msgBoxMain.Show(string.Format("An error was logged: {0}", ex.Message.ToString()), MessageBox.PbKMessageType.Error);
            }
        }

private AutoCompleteExtender getAutoCompleteExtender(string id, string clientId)
        {
            var ace = new AutoCompleteExtender();
            string[] splitStr = {"txt"};
            var cid = clientId.Split(splitStr,StringSplitOptions.None)[0];
            //ace.ID = cid + "ace" + id;
            ace.ID = "ace" + id;
            //ace.BehaviorID = cid + "ace" + id + "BehaviourCode";
            //ace.TargetControlID = clientId;
            ace.ServiceMethod = "SearchByAttorneyBarCodeORNameNoJQ";
            ace.ServicePath = "../WebService/SearchCharge.asmx";
            ace.MinimumPrefixLength = 2;
            ace.CompletionInterval = 100;
            ace.EnableCaching = true;
            ace.CompletionSetCount = 20;
            ace.UseContextKey = true;
            ace.ContextKey = "J";
            ace.CompletionListCssClass = "AutoExtenderList";
            ace.CompletionListItemCssClass = "AutoExtenderListItem";
            ace.CompletionListHighlightedItemCssClass = "AutoExtenderHighlightItem";
            ace.ShowOnlyCurrentWordInCompletionListItem = true;
            ace.FirstRowSelected = false;
            ace.OnClientItemSelected = "GetSelected" + id;
            ace.OnClientPopulated = "HighlithSearchTerm";
            ace.CompletionListElementID = "divAuthoCompListItemContainerFor" + id;
            return ace;
        }

0 个答案:

没有答案