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开发者_如何学JAVA selects a different TreeNode from my Treeview.
After some searching, What could possibly be the solution:- Add and raise an event from the user control that fires when the Treeview selected node changes. Creating a custom event argument that contains the node value, allows it to get passed directly to the event handler.
If so, can you show me a basic working example which implements this approach?
Thanks.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
}
精彩评论