Home ProgrammingASP.NET Implement Same WebMethod for AutoComplete in Multiple Textbox

Implement Same WebMethod for AutoComplete in Multiple Textbox

by John Bhatt
0 comment 454 views

Hi,While Answering a question at Forum, I wrote this post.

Suppose We have Multiple Textbox in Same Page and we want to Implement AutoSuggest / AutoComplete feature in all TextBox and of same type. Suppose Multiple Google Search Boxes.

For that case, we have separate Textboxs, Separate AutoComplete Extenders and Separate Methods. We are trying to Consume same WebMethod in All Textbox.

Front End Code:

<asp:TextBox ID="TextBox1" runat="server" ToolTip="Product One" Width="168px"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" DelimiterCharacters="" Enabled="True" ServiceMethod="GetProducts" MinimumPrefixLength="2"
ServicePath="" TargetControlID="TextBox1" UseContextKey="True"></asp:AutoCompleteExtender>

<asp:TextBox ID="TextBox2" runat="server" ToolTip="Product Two" Width="168px"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox3_AutoCompleteExtender" runat="server" DelimiterCharacters="" Enabled="True" ServiceMethod="GetProducts" MinimumPrefixLength="2"
ServicePath="" TargetControlID="TextBox2" UseContextKey="True"></asp:AutoCompleteExtender>

<asp:TextBox ID="TextBox3" runat="server" ToolTip="Product Three" Width="168px"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox3_AutoCompleteExtender" runat="server" DelimiterCharacters="" Enabled="True" ServiceMethod="GetProducts" MinimumPrefixLength="2"
ServicePath="" TargetControlID="TextBox3" UseContextKey="True"></asp:AutoCompleteExtender>

BackEnd Code:

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetProducts(string prefixText, int count, string contextKey)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToStore"].ConnectionString);
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Products where UnitsInStock>0 and ProductName like '%" + prefixText + "%'", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        string[] Products = new string[ds.Tables[0].Rows.Count];
        for (int j = 0; j < = ds.Tables[0].Rows.Count - 1; j++)
        {
            Products.SetValue(ds.Tables[0].Rows[j][1].ToString(), j);
        }
        return Products;
    }

Lets Look at the Output.
WebMethodWe got the desired output using same WebMethod in multiple locations in a web page.
Hope this will be Helpful.

0 comment

Related Articles

Leave a Reply

Do NOT follow this link or you will be banned from the site!