Kamyar
Kamyar

Reputation: 18797

Updating a web user control based on another web user control

I have a web user control which has a Treeview control inside it. I have created another user control which contains a Gridview along with a couple of other controls.

The Gridview, should update itself whenever the user selects a different TreeNode from my Treeview.
After some searching, What could possibly be the solution:

If so, can you show me a basic working example which implements this approach?
Thanks.

Upvotes: 4

Views: 625

Answers (1)

CRice
CRice

Reputation: 12567

You could let your main page code behind handle a custom event from the Treeview control. Then in the event handler call a public method in the gridview control.

If control1 is your tree control and control2 is your grid control:

Main Page aspx (set control1 event handler to a method in this page):

<%@ Register Src="~/Controls/WebUserControl1.ascx" TagName="Control1" TagPrefix="ctrl" %>
<%@ Register Src="~/Controls/WebUserControl2.ascx" TagName="Control2" TagPrefix="ctrl" %>
<ctrl:Control1 ID="control1" runat="server" OnTreeNodeChanged="Control1_TreeNodeChanged" />
<ctrl:Control2 ID="control2" runat="server" />

Main Page code behind:

    public void Control1_TreeNodeChanged(object sender, EventArgs e)
    {
        control2.ReloadGrid();
    }

Tree control code

public event EventHandler TreeNodeChanged;

protected void FromYourTreeNodeEvent(object o, EventArgs e)
{
    //fire your custom event

    if (TreeNodeChanged!= null)
    {
        TreeNodeChanged(this, EventArgs.Empty);
    }            
}

Grid control code

    public void ReloadGrid()
    {
        //do something
    }

Upvotes: 3

Related Questions