Question

I have a composite user control with dynamically created text boxes. As the text boxes are created and inserted into a placeholder on the ASCX, I'm also dynamically creating a AutoCompleteExtender, targeting the text box just created. Everything works fine if I only add a single ACE, but as soon as more than one are present on the page, I get the following error:

Microsoft JScript runtime error: Unable to get value of the property '_behaviors': object is null or undefined

This is the specific location of the JScript error, contained within jQuery.

var c=a._behaviors=a._behaviors||[];

The same code works if I create dummy text boxes and ACE's on my page. But I need these created in the custom control.

That indicates I'm "doing it right" - also, since I can get ONE ACE working in the control just fine.

I'm using a web service - NOT a page method - I realize user and custom controls cannot contain page methods, those must be in a "page."

I've tried a number of things - I've ensured the text boxes all have unique ID's. The ACE's all have unique ID's. I've tried with and without the BehaviorID on the ACE defined (again with a unique ID). I know the web service works because a single ACE runs great.

I've even tried passing a list of the generated ACE's up to the page level from the control, and inserting them into a page-level placeholder. Then I get RTE's that the textbox could not be found with the ID provided.

Any tips for adding multiple ACE's in a composite user control, with dynamically generated text boxes and extenders?

Regards.

Was it helpful?

Solution

This is working for me:

public partial class multiACEfromCodeBehind : System.Web.UI.Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {   
            for(int i = 0; i < 10; i++)
            {
                // Create TextBox and its properties
                string textBoxID = String.Format("{0}_{1}", "AutoCompleteTextBox", i);
                TextBox textbox = new TextBox();
                textbox.ID = textBoxID;
                textbox.Width = new Unit(250);
                textbox.Attributes.Add("autocomplete", "off");

                // Create AutoCompleteExtender and its properties
                AutoCompleteExtender autoCompleteExtender = new AjaxControlToolkit.AutoCompleteExtender();
                autoCompleteExtender.TargetControlID = textBoxID;
                autoCompleteExtender.ServiceMethod = "GetCompletionList";
                autoCompleteExtender.ServicePath = "YourAutoCompleteWebService.asmx";
                autoCompleteExtender.CompletionInterval = 1500;
                autoCompleteExtender.CompletionSetCount = 10;
                autoCompleteExtender.EnableCaching = true;

                // Add created controls to the page controls collection
                this.Controls.Add(textbox);
                this.Controls.Add(autoCompleteExtender);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top