开发者

Can WebUserControls get type infomation about other WebUserControls within the project?

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 开发者_StackOverflow中文版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...


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜