【问题标题】:How do you pass a another control into a user-created ASP.NET control as a parameter in its decleration using the ID of the other control?如何使用另一个控件的 ID 将另一个控件作为参数传递到用户创建的 ASP.NET 控件中?
【发布时间】:2026-01-07 08:35:01
【问题描述】:

我正在尝试创建一个控件 (MyControl),该控件对其所在页面上的其他控件执行操作。它有一个属性:

public property System.Web.UI.Control SomeOtherControl;

我希望能够从使用该控件的 ASPX 页面设置此属性。现在,我能做到的唯一方法是在代码隐藏的Page_Load 中:

MyControl.SomeOtherControl = TheOtherControl;

我不想在每个使用 MyControl 的页面的代码隐藏中执行此操作,而是只想在 ASPX 页面上执行此操作:

<xyz:MyControl SomeOtherControl="TheOtherControlsID" />

为了做到这一点,我尝试在 ID 字符串上使用 Page.FindControl() 方法来在 MyControl 初始化时找到引用的控件,但它只搜索页面的*控件。我可以编写自己的递归FindControl(),但我认为必须有更好的方法来做到这一点,我担心这样的事情的性能。有什么想法吗?

【问题讨论】:

    标签: asp.net user-controls parameter-passing


    【解决方案1】:

    递归 FindControl 可能比您想象的要快得多。即使有数百个控件,我什至无法衡量处理时间的毫秒级差异。

    【讨论】:

      最近更新 更多