ASCX 用户控件中的 FindControl

发布于 2024-10-25 07:02:24 字数 396 浏览 5 评论 0原文

我试图在我的 ascx 文件中找到一个我设置为在服务器上运行的 DIV 控件,但是当我调试它时,我得到 findcontrol 的值为空,所以它没有找到它,我做错了什么?

这是从我的 ASPX 页面调用的:

        HtmlGenericControl div = (HtmlGenericControl)FindControl("search");
        div.Visible = false;

我的 ASCX 代码:

<div class="contactsearch" id="search" runat="server" visible='true'>
//mycontent
</div>

I am trying to find a control a DIV that I have set to runat server in my ascx file, but when I debug it, I get the value of findcontrol is null so its not finding it, what am I doing wrong?

This being called from my ASPX page:

        HtmlGenericControl div = (HtmlGenericControl)FindControl("search");
        div.Visible = false;

My ASCX Code:

<div class="contactsearch" id="search" runat="server" visible='true'>
//mycontent
</div>

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

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

发布评论

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

评论(3

2024-11-01 07:02:24

FindControl 仅搜索第一个子级,它不会递归到控制树中,使用如下内容:

http:// /stevesmithblog.com/blog/recursive-findcontrol/

或这个

http: //ra-ajax.org/jquery-ish-selector-for-webcontrols

FindControl searches only first childs, it doesn't go recursive into control tree, use something like this:

http://stevesmithblog.com/blog/recursive-findcontrol/

or this

http://ra-ajax.org/jquery-ish-selector-for-webcontrols

甜中书 2024-11-01 07:02:24

您可以使用递归算法进行搜索,如马库斯特和安东尼奥的答案中所讨论的那样,它在这种情况下将起作用。然而,它有一些问题。例如,如果页面上有两个用户控件实例,它将不起作用。

然而,更大的问题是您破坏了用户控件提供的封装。包含页面不应该知道用户控件包含名为“txtSearchCriteria”的文本框或名为“btnSearch”的按钮。 (或名为“search”的

。)包含页面只能使用用户控件公开公开的接口与用户控件一起使用。

我建议您在用户控件中创建一个属性(或一组属性),以允许使用者以您期望的方式与控件进行交互。例如:

Public Boolean SearchControlsVisible
{
    get { return search.Visible; }
    set { search.Visible = value; }
}

即使页面上有多个用户控件实例,该属性的代码也可以毫无歧义地访问“search”

。当您将控件放在页面上时,此方法还使您能够在 ASPX 标记中设置这些属性:

<my:ContactSearchPanel ID="contactSearch runat="server"
    SearchControlsVisible="false"
    ...etc... />

这不是您的问题,但您还需要响应用户控件中发生的事件。有关从用户控件引发事件的说明,请参阅此页面:http://msdn.microsoft.com/en-us/library/wkzf914z(v=vs.71).aspx

创建并公开事件后,您可以在标记中附加一个处理程序,例如这:

<my:ContactSearchPanel ID="contactSearch runat="server"
    SearchControlsVisible="false"
    OnSearchClicked="SearchPanel_SearchClicked"
    ...etc... />

You can search using a recursive algorithm, as discussed in Markust and Antonio's answers, and it will work in this instance. However, it has a few problems. For example, it won't work if you have two instances of the usercontrol on your page.

However, the bigger problem is that you're breaking the encapsulation that a usercontrol provides. The containing page should not know that a usercontrol contains, say, a Textbox named "txtSearchCriteria," or a button named "btnSearch." (Or a <div> named "search.") The containing page should only work with the usercontrol using the interface that the usercontrol exposes publicly.

I recommend that you create a property (or set of properties) in the usercontrol to allow consumers to intreact with the control in ways that you expect them to. For example:

Public Boolean SearchControlsVisible
{
    get { return search.Visible; }
    set { search.Visible = value; }
}

The property's code can access the "search"<div> without ambiguity, even if you have multiple instances of the usercontrol on the page. This approach also gives you the ability to set those properties in the ASPX markup when you place the control on the page:

<my:ContactSearchPanel ID="contactSearch runat="server"
    SearchControlsVisible="false"
    ...etc... />

It's not in your question, but you'll need to respond to events that occur in the usercontrol as well. For instructions on raising events from a usercontrol, see this page: http://msdn.microsoft.com/en-us/library/wkzf914z(v=vs.71).aspx

Once you've created and exposed an event, you can attach a handler in the markup like this:

<my:ContactSearchPanel ID="contactSearch runat="server"
    SearchControlsVisible="false"
    OnSearchClicked="SearchPanel_SearchClicked"
    ...etc... />
流殇 2024-11-01 07:02:24

首先检查 contactsearch 控件是否存在于控件树层次结构中。您可以通过简单地浏览控件的 Controls 属性来完成此操作。如果存在,则需要递归控制搜索才能找到它。

编辑:被安东尼奥击败:P

First check that contactsearch control is there in the control tree hierarchy. you can do this by simply exploring the Controls property of the control. If it's there, you need a recursive control search in order to find it.

Edit: Beaten by Antonio :P

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