Follow the link with one user

I have two user controls on the same page. One contains a ListView that displays navigation links, the second user control should be updated when the user clicks the Buttonlink button in the ListView. How can i do this?

+4
source share
1 answer
  • UserControl A should handle the button click and raise the user event declared in UserControl
  • The page processes this event and calls the public UserControl B method, which updates its contents.

You can pass the necessary information from UserControl A to the page via EventArgs (or pass UserControl itself as an argument and use its public properties).

The page then passes the arguments to UserControl B through a method parameter or by changing its public properties before calling Update -Method.


Here is an example of the code you requested. Sorry for the meaningless naming, but you did not say what it all means. Instead, you should use readable variables, properties, a method, and event names.

Reduced UserControl A using ListView:

 <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="UsercontrolA.ascx.vb" Inherits="WebApplication1.UserControlA" %> <asp:ListView ID="ListView1" runat="server"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" CommandName="LinkClick" CommandArgument='<%#Eval("ID") %>' runat="server" Text='<%#Eval("Text") %>'></asp:LinkButton> </ItemTemplate> </asp:ListView> 

Removed ListView data binding from codebehind because it doesn't matter. The important part is handling the ListView ItemCommand and raising the custom event:

 Public Event LinkClicked(sender As UserControlA, id As Int32) Private Sub LV_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListView1.ItemCommand If e.CommandName = "LinkClick" Then Dim id = CType(e.CommandArgument, Int32) ' This is the best way for UC to commmunicate with the page: ' RaiseEvent LinkClicked(Me, id) End If End Sub 

A simple UserControl B with nothing more than a label (ascx):

 <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="UserControlB.ascx.vb" Inherits="WebApplication1.UserControlB" %> <asp:Label ID="Label1" runat="server"></asp:Label> 

Using Update -Method in codebehind:

 Public Sub Update(showID As Int32) Me.Label1.Text = String.Format("Link {0} clicked", showID.ToString) End Sub 

Finally, here is the Page (aspx)

 <uc1:UsercontrolA ID="UC_A" runat="server" /> <br /> <uc2:UserControlB ID="UC_B" runat="server" /> 

It controls both UserControls. It processes the event from UserControl A and calls the Update method, which provides UserControl B:

 Private Sub LinkClicked(sender As UserControlA, id As Integer) Handles UC_A.LinkClicked Me.UC_B.Update(id) End Sub 

Advantage this event-approach is that UserControls remain reusable. You can use UserControl A on other pages, even if they do not handle this event. This is part of the controller to decide what is needed and what needs to be done.

UserControls, as a rule, should not depend on specific controllers, otherwise they are rigidly connected and cannot be reused. It would also be a good source of unpleasant erros. UserControl can be a controller for other nested (User-) controls, but not for the page itself.

Communication summary :

  • Page -> UserControl -> Public Properties and Methods
  • UserControl -> Page -> Events
  • UserControl -> UserControl -> Controller-UserControl takes the role of the page (see above)
+6
source

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


All Articles