ASP.Net在FormView控件中访问子控件 [英] ASP.Net Accessing child controls in a FormView control

查看:117
本文介绍了ASP.Net在FormView控件中访问子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有EditItemTemplate的FormView控件(myFormView),该控件包含许多子控件.当我使用标准的ASP.Net DropDownList控件(myDropList)时,可以使用以下行获取对myDropList的引用:

I'm using a FormView control (myFormView) with an EditItemTemplate which contains a number of child controls. When I use a standard ASP.Net DropDownList control (myDropList), I can obtain a reference to myDropList using the line below:

((DropDownList)myFormView.FindControl("myDropList"))

我可以完全访问myDropList的属性并获取当前选择的值.太好了.

I can full access the properties of the myDropList and obtain the value currently selected. This is great.

但是,我现在需要使用第三方子控件(如FreeTextBox,位于此处 http://www.freetextbox.com).我已经将FreeTextBox控件称为myFTB,并且使用的是与上述类似的语句:

However, I now need to use a 3rd party child control (FreeTextBox as found here http://www.freetextbox.com) in the FormView control. I've called the FreeTextBox control myFTB and I'm using a similar statement as above:

((FreeTextBox)myFormView.FindControl("myFTB"))

但是,它返回null,因此我可以为此检索属性值.

However, this returns null and thus I'm enable to retrieve property values for this.

有人知道为什么它返回null吗?还有其他方法可以检索对控件的引用吗?

Does anyone know why it is returning null? Is there some other way to retrieve the reference to the control?

TIA

推荐答案

您将需要使用递归在控件层次结构中找到控件.

You will need to use recursion to find the control in the controls hierarchy.

尝试使用以下方法:

FreeTextBox textBox = (FreeTextBox)FindControl(myFormView, "myFTB");

...

private Control FindControl(Control parent, string id)
{
    foreach (Control child in parent.Controls)
    {
        string childId = string.Empty;
        if (child.ID != null)
        {
            childId = child.ID;
        }

        if (childId.ToLower() == id.ToLower())
        {
            return child;
        }
        else
        {
            if (child.HasControls())
            {
                Control response = FindControl(child, id);
                if (response != null)
                    return response;
            }
        }
    }

    return null;
}

这篇关于ASP.Net在FormView控件中访问子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆