Updating a web user control based on another web user control

I have a web user control that has a Treeview control in it. I created another user control that contains a Gridview along with several other controls.

Gridview should be updated every time the user selects another TreeNode from my Treeview.
After some searching, what might be the solution:

  • Add and raise an event from a user control that fires when the selected node tree changes. Creating a custom event argument containing the node value allows you to pass it directly to the event handler.

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

+4
source share
1 answer

You can leave your main page code behind the custom event descriptor from the Treeview control. Then, in the event handler, call the public method in the gridview control.

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

Aspx main page (set control1 event handler for the method on 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 management code

 public event EventHandler TreeNodeChanged; protected void FromYourTreeNodeEvent(object o, EventArgs e) { //fire your custom event if (TreeNodeChanged!= null) { TreeNodeChanged(this, EventArgs.Empty); } } 

Network management code

  public void ReloadGrid() { //do something } 
+3
source

Source: https://habr.com/ru/post/1339530/


All Articles