2

I have created Ajax autocomplete extender on my web page that retrieves the values from the database. When I select one of the options from the autocomplete dropdown list I get the desired result. The problem is:

When the user types entire word by himself (or if the word is pasted) and when he does not select one of the options from the dropdownlist and presses enter, result is blank. How do I solve this problem?

My code for autocomplete extender is:

<asp:HiddenField ID="HiddenID" runat="server" />
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            <asp:TextBox ID="SearchRetailer" runat="server" Style="background-image: url(Search-icon.png ); border: 1px groove black; background-position: right; background-repeat: no-repeat; float: right; width: 20%;" />
            <asp:AutoCompleteExtender
                ID="AutoCompleteExtender1"
                TargetControlID="tgt1"
                runat="server" MinimumPrefixLength="2" ServiceMethod="GetCompletionList" UseContextKey="True" OnClientItemSelected="GetCode" EnableCaching="true" />

The javascript function is:

  <script type="text/javascript" language="javascript">

    function GetCode(source, eventArgs) {
        var hfield = $get('<%=this.HiddenID.ClientID%>');
        hfield.value = eventArgs.get_value();
        window.open("newPage.aspx?Paramter1=" + hfield.value + "", "_self", false);
    }

</script>

And my webservice code is:

 [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{

    SqlConnection sqlconn = "my connection string"

    string sql = "select abc from aaa where abc like '" + prefixText + "%'";
    SqlDataAdapter cmd1 = new SqlDataAdapter(sql, sqlconn);
    DataTable dt0r = new DataTable();
    cmd1.Fill(dt0r);
    int i = 0;
    string[] rList = new string[dt0r.Rows.Count];        
    foreach (DataRow dr in dt0r.Rows)
    {
        rList.SetValue(dr["abc"].ToString(), i);

        i++;
    }
    return rList;

}
4

1 回答 1

0

我让它工作。

我在自动完成扩展器的属性中添加了 FirstRowSelected="true" 并且该功能现在可以工作了!

于 2017-02-28T21:55:03.110 回答