WebUserControls 能否获取有关项目内其他 WebUserControls 的类型信息?

发布于 2024-10-16 17:03:17 字数 416 浏览 2 评论 0原文

我基本上是想让网络控件相互交谈。

我有一个 WUC 想从另一个 WUC 获取信息。

在 ControlB 的 GetDataFromControlA 方法中

public List<ControlBData> GetDataFromControlA()
{
    ControlA c = Page.FindControl( IdOfControlA) as ControlA;
    if( c != null)
    {
        return c.Data;
    }
   ...
}

在代码时 ControlB 对 ControlA 一无所知...因此 ControlB 无法访问其成员,并且上述内容无法编译。

我想我必须使用页面中的方法来让控件相互通信......

I'm basically trying to get webcontrols to talk to one another.

I have one WUC that wants to get information from another.

In ControlB's GetDataFromControlA method

public List<ControlBData> GetDataFromControlA()
{
    ControlA c = Page.FindControl( IdOfControlA) as ControlA;
    if( c != null)
    {
        return c.Data;
    }
   ...
}

At code time ControlB knows nothing of ControlA...so ControlB cant access its members, and the above wont compile.

I'm thinking I have to use methods in the page to get the controls talking to one another...

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

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

发布评论

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

评论(2

表情可笑 2024-10-23 17:03:17

我缺少 <%@ Reference Control="~/ControlA.ascx" %>在 ControlB.ascx 中,

这“修复”了智能和编译错误! 在这里找到

I was missing the <%@ Reference Control="~/ControlA.ascx" %> in ControlB.ascx

This 'fixes' the intellisence and the compile error! Found here

我很OK 2024-10-23 17:03:17

是的,您可以使用方法来实现控件之间的一些通信(因为每个控件最终都属于一个类)。

但您必须将 Control 显式转换为您自己的 WUC 数据类型。

例子?

// Convert it from Control to Button
Button nextBtn = (Button)this.FindControl("ButtonIDHere");

// Make sure it is there (not null)
if (nextBtn != null)
{
    // Finally, let your logic does the magic!
    nextBtn.OnClientClick = "showPopup();";

    // Notice that here you can get the specific control members in addition to its base classes' members
}

在这种情况下你不应该使用static,它会让你很头疼。

如果 WUC 在另一个页面中,只需获取对该页面的引用即可。

希望有帮助。

Yes, you can use methods to achieve some communication between controls (since each belongs to a class in the end).

but you have to explicitly cast the Control to your own WUC's Datatype.

Example?

// Convert it from Control to Button
Button nextBtn = (Button)this.FindControl("ButtonIDHere");

// Make sure it is there (not null)
if (nextBtn != null)
{
    // Finally, let your logic does the magic!
    nextBtn.OnClientClick = "showPopup();";

    // Notice that here you can get the specific control members in addition to its base classes' members
}

You shouldn't use static in such case, it will get you a lot of headache.

If the WUC were in another Page, just get a reference to that page.

Hope that helps.

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