Implement Same WebMethod for AutoComplete in Multiple Textbox

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 PageMethod in All Textbox.


Front End Code:

Product One:

Product Two:

Product Three:




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.

Hope this will be Helpful.
John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

Reactions: 

DropDownList (Country-State-District-City)

How to Make a Complete Dropdown and Prevent Inserting Dummy Data from Form.
Disable other Controls when Clicked back to Parent Control:
Drop Down Sequence:
OnLoad
Load Country
On-Change
Load State according to Country
On-Change
Load Dists in State
On-Change
List Cities in Dist.

Code:
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlState.DataSource = res.States(Convert.ToInt16(ddlCountry.SelectedValue));
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateID";
        ddlState.DataBind();
        ddlState.Items.Insert(0, "Select");
        ddlDist.Enabled = false;
        ddlCity.Enabled = false;
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlDist.DataSource = res.Districts(Convert.ToInt16(ddlState.SelectedValue));
        ddlDist.DataTextField = "DistName";
        ddlDist.DataValueField = "DistID";
        ddlDist.DataBind();
        ddlDist.Items.Insert(0, "Select");
        ddlDist.Enabled = true;
        ddlCity.Enabled = false;
    }

    protected void ddlDist_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlCity.DataSource = res.Cities(Convert.ToInt16(ddlDist.SelectedValue));
        ddlCity.DataTextField = "CityName";
        ddlCity.DataValueField = "CityID";
        ddlCity.DataBind();
        ddlCity.Items.Insert(0, "Select");
        ddlCity.Enabled = true;
    }
John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

Reactions: