views:

321

answers:

1

I'm currently trying to add a FilteredTextBoxExtender in a ASP.NET composite control, but I can't get it to work.

I have the following code within the overridden CreateChildControls method:

var textbox = new TextBox { ID = string.Format("{0}_textbox", ID);
var filteredTextBoxExtender = new FilteredTextBoxExtender { ID = string.Format("{0}_filter", ID), TargetControlID = textbox.ID, FilterType = FilterTypes.Numbers };

Controls.Add(textbox);
Controls.Add(filteredTextBoxExtender);

Somehow, it looks like it is added correctly when I view the source of my test page, but it won't fire the filtering

Sys.Application.add_init(function() {
$create(AjaxControlToolkit.FilteredTextBoxBehavior, {"FilterType":2,"id":"ctl00_testTextBox_testTextBox_filter"}, null, null, $get("ctl00_testTextBox_testTextBox_textBox"));
});

...

<input id="ctl00_testTextBox" name="ctl00$testTextBox$testTextBox_textBox" type="text" value="" id="ctl00_testTextBox_testTextBox_textBox" />

Any idea what goes on here, and how to get it to fire the filtering?

edit: I've narrowed it down to a matter of dynamically choosing which controls to render. The FilteredTextBoxExtender works if I explicitely render each child control, but not if I don't render one of the child controls in the composite control (a label control)

+1  A: 

Try this:

The problem is the textbox's id. If you check the generated JavaScript, you can sense, the input id is different from assigned id to $get("ctl00_testTextBox_testTextBox_textBox"). So your code doesn't work. You should mark your composite control by INamingContainer marker inerface. Thus, the composite control's id combines with the added textbox id and generates an unique identity. It resolves the problem.

public class Foo : CompositeControl
{

    protected override void CreateChildControls()
    {
        var textbox = new TextBox { ID = "textbox" }
        var filteredTextBoxExtender = new FilteredTextBoxExtender { TargetControlID = textbox.ID, FilterType = FilterTypes.Numbers };

        Controls.Add(textbox);
        Controls.Add(filteredTextBoxExtender);
    }

}
Mehdi Golchin
Thanks, but INamingContainer is redundant because my control already inherits from CompositeControl.I've edited my question with some more information that I have discovered since the post
Geir-Tore Lindsve
Yea, the `CompositeControl` has already implemented `INamingContainer`. I'm going to edit my post. However, I can't realize the updated part of your question.
Mehdi Golchin