Add AutoCompleteExtender to dynamically created control problem

Last post 11-20-2007 7:13 AM by techmail. 3 replies.

Sort Posts:

  • Add AutoCompleteExtender to dynamically created control problem

    06-26-2006, 8:56 AM
    • Member
      40 point Member
    • mcdeveloper
    • Member since 06-26-2006, 12:29 PM
    • Posts 8

    Hi,

    I'm trying to add AutoComplete behavior to a dynamically created TextBox that's in a custom server control. At first, I used the following code which worked fine:

    string js = "<script type=\"text/xml-script\">" +
                                 "<page xmlns:script=\"http://schemas.microsoft.com/xml-script/2005\">" +
                                 "<components>" +
                                 "<textBox id=\"" + TextBox1.ClientID + "\">" +
                                 "<behaviors>" +
                                 "<autoComplete completionList=\"" + completionList.ClientID + "\"" +
                                 " serviceURL=\"WebService.asmx\"" +
                                    " serviceMethod=\"" + webserviceMethod + "\"" +
                                    " minimumPrefixLength=\"2\"" +
                                    " completionSetCount=\"15\"" +
                                    " completionInterval=\"500\" />" +
                                   "</behaviors>" +
                               "</textBox>" +
                           "</components>" +
                         "</page>" +
                       "</script>";
                   
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "AutoComplete_" + TextBox1.ClientID, js);

    But then, the control got nested into an UpdatePanel, and the autocomplete behavior was gone. Hence, I tried to add the AutoCompleteExtender by adding it as a LiteralControl like this:

    Panel panTextBox = new Panel();

    panTextBox.Controls.Add(new LiteralControl("<atlas:AutoCompleteExtender ID=\"" + "AutoComplete_" + TextBox1.ClientID + "\" ServicePath=\"~/WebService.asmx\" ServiceMethod=\"" + webserviceMethod + "\" MinimumPrefixLength=\"2\" runat=\"server\">" + "<atlas:AutoCompleteProperties TargetControlID=\"" + TextBox1.ClientID + "\" Enabled=\"true\" />" +"</atlas:AutoCompleteExtender>") );

    this

    .Controls.Add(panTextBox); //this = custom server control

    That doesn't work, even though I removed the UpdatePanel. Can anybody help me adding auto complete behavior to the TextBox control?
  • Re: Add AutoCompleteExtender to dynamically created control problem

    06-26-2006, 11:30 AM
    • Member
      40 point Member
    • mcdeveloper
    • Member since 06-26-2006, 12:29 PM
    • Posts 8

    Problem solved Big Smile

    The trick is, to manually add an ID to the TextBox control, like this:

    TextBox1.ID = "textbox_" + ID;

    Ok, instead of using this LiteralControl construction, I used the AutoCompleteExtender object from Microsoft.Web.UI.Controls, like this:

    AutoCompleteProperties acp = new AutoCompleteProperties();
    acp.Enabled = true; //Do NOT forget, false by default!
    acp.TargetControlID = TextBox1.ID; //NOTE THIS

    AutoCompleteExtender ace = new AutoCompleteExtender();

    ace.ID =
    "AutoCompleteExtender_" + ID;
    ace.ServiceMethod = webserviceMethod;
    ace.MinimumPrefixLength = 2;
    ace.ServicePath =
    "Webservice.asmx";
    ace.TargetProperties.Add(acp);

    panTextBox.Controls.Add(ace);

    So don't use the TextBox.ClientID, as the AutoCompleteExtender won't be able to find the control. Luckily, the AutoCompleteExtender complained about not being able to find the textbox control when it was added to the page at runtime. That put me on the right track.

  • Re: Add AutoCompleteExtender to dynamically created control problem

    06-26-2006, 11:35 AM
    • Member
      40 point Member
    • mcdeveloper
    • Member since 06-26-2006, 12:29 PM
    • Posts 8

    Before I forget, the "ID" variable of "textbox_" + ID was created by myself, actually it's an integer that increases with every control that is dynamically added.

  • Re: Add AutoCompleteExtender to dynamically created control problem

    11-20-2007, 7:13 AM
    • Member
      2 point Member
    • techmail
    • Member since 08-03-2007, 8:04 AM
    • Posts 12

    I'm also in the situation described above. I do create controls and assign ids to them, but then i need them in javascript to do some calculations. Please any solution to original problem of finding clientid of dynamically created controls.

Page 1 of 1 (4 items)