ASP.Net 访问 FormView 控件中的子控件

发布于 2024-12-11 05:48:20 字数 614 浏览 3 评论 0原文

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

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

我可以完全访问 myDropList 的属性并获取当前选定的值。这太棒了。

但是,我现在需要使用第 3 方子控件(此处找到的 FreeTextBox http://www.freetextbox.com) 在 FormView 控件中。我将 FreeTextBox 控件称为 myFTB,并且使用与上面类似的语句:

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

但是,这会返回 null,因此我能够检索其属性值。

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

TIA

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"))

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

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"))

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

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

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2024-12-18 05:48:20

您将需要使用递归来查找控件层次结构中的控件。

尝试使用以下方法:

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

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

Try using the following method:

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;
}
少女净妖师 2024-12-18 05:48:20

您可以这样做来查找表单视图中的控件...

注意:下面的代码查找表单视图控件中的所有文本框

 protected void FormView1_DataBound(object sender, EventArgs e)
 {
        if (FormView1.CurrentMode == FormViewMode.Edit)
        {
            FindAllTextBoxes(FormView1);
        }
 }

 private void FindAllTextBoxes(Control parent)
 {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox tbox = c as TextBox;
                if (tbox != null)
                {
                    // textbox found ....you could send this textbox, by reference to another procedure that assigns the values comparing
                    //it by tbox.ID
                }
            }
            if (c.Controls.Count > 0)
            {
                FindAllTextBoxes(c);
            }
        }
  }

我希望它会对您有所帮助。

you can do like this to find controls in form view ....

NOte: The below code find the all text boxes inside the form view control

 protected void FormView1_DataBound(object sender, EventArgs e)
 {
        if (FormView1.CurrentMode == FormViewMode.Edit)
        {
            FindAllTextBoxes(FormView1);
        }
 }

 private void FindAllTextBoxes(Control parent)
 {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox tbox = c as TextBox;
                if (tbox != null)
                {
                    // textbox found ....you could send this textbox, by reference to another procedure that assigns the values comparing
                    //it by tbox.ID
                }
            }
            if (c.Controls.Count > 0)
            {
                FindAllTextBoxes(c);
            }
        }
  }

I hope it will helps you..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文