开发者

Programmatically add a FilteredTextBoxExtender within a composite control

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_t开发者_JS百科estTextBox_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)


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);
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜