Saturday, 25 August 2012

Using Autocomplete Extender passing an extra parameter through the contextKey.

I'm trying to pass a static value (from a list box) into an Autocomplete Extender.   I've had Autocomplete working with just the prefixText but can not get it to work when passing an extra parameter through the contextKey.

Please see below:

On Page Load:
 AutoCompleteExtender1.ContextKey = lstUserID.SelectedValue

(the 'lstUserID' holds the ID of the group logged in)

the autoComplete Extender:

<cc1:AutoCompleteExtender
                    ServiceMethod="SearchNino"
                    MinimumPrefixLength="2"
                    CompletionInterval="100"
                    EnableCaching="false"
                    CompletionSetCount="10"
                    TargetControlID="txtNinoSearch"
                    ID="AutoCompleteExtender1"
                    runat="server"
                    FirstRowSelected = "false"
                    UseContextKey="True"
                    >

Here's the method..
<System.Web.Script.Services.ScriptMethod(), _
  System.Web.Services.WebMethod()> _
 Public Shared Function SearchNINO(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As List(Of String)

        Dim conn As SqlConnection = New SqlConnection
        conn.ConnectionString = ConfigurationManager _
         .ConnectionStrings("employersConnectionString").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandText = "select NINO from table where" & _
         " ((NINO like @SearchText + '%') AND (Emp_Code = contextKey)) Group By NINO"
        cmd.Parameters.AddWithValue("@SearchText", prefixText)
        cmd.Connection = conn
        conn.Open()

        Dim NINO As List(Of String) = New List(Of String)
        Dim sdr As SqlDataReader = cmd.ExecuteReader
        While sdr.Read
            PayRef.Add(sdr("NINO").ToString)
        End While
        conn.Close()
        Return NINO

    End function
 
C# Code: 

 <asp:ToolkitScriptManager runat="server" ID="TSM">
        </asp:ToolkitScriptManager>
        <script type="text/javascript">
            function pageLoad(sender, e) {
                var o = $find("ACE1");
                o.add_populating(function () {
                    o.set_contextKey($get("<%=TxtPara1.ClientID %>").value + "|" + $get("<%=TxtPara2.ClientID %>").value);
                });
            }
        </script>
        para1:
        <asp:TextBox runat="server" ID="TxtPara1"></asp:TextBox><br />
        para2:
        <asp:TextBox runat="server" ID="TxtPara2"></asp:TextBox><br />
        AutoCompleteTextBox:
        <asp:TextBox runat="server" ID="auto"></asp:TextBox>
        <asp:AutoCompleteExtender runat="server" ID="ACE1" MinimumPrefixLength="1" TargetControlID="auto"
            UseContextKey="true" ServicePath="service.asmx" ServiceMethod="AutoMethod">
        </asp:AutoCompleteExtender>
 Webservice
 [WebMethod]
    public static string[] AutoMethod(string prefixText, int count, string contextKey)
    {
        string[] paras = contextKey.Split('|');
        List<string> rsl = new List<string>();
        rsl.Add("para1:" + paras[0]);
        rsl.Add("para2:" + paras[1]);
        return rsl.ToArray();
    } 

No comments:

Post a Comment