Aaron
Aaron

Reputation: 55

Pass Link-Button Click from One User Control to Another

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 user clicks on the buttonlink in the ListView. How can I do this?

Upvotes: 4

Views: 2282

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460208

  1. UserControl A should handle the button-click and raise a custom event declared in the UserControl
  2. The page handles this event and calls a public method of UserControl B that updates it's content

You could pass necessary informations from UserControl A to page via EventArgs(or pass the UserControl itself as argument and use it's public properties).

The page then passes the arguments to UserControl B via method parameter or by changing it's public properties before calling the Update-Method.


Here is the sample code you've requested. Sorry for the meaningless naming but you haven't told what's this all about. You should use readable variables,properties,method and event-names instead.

Reduced UserControl A with a 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 the ListView databinding from codebehind because that doesn't matter. The important part is handling the ListView's 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's to commmunicate with the page: '
        RaiseEvent LinkClicked(Me, id)
    End If
End Sub

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>

With an 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 handles the event from UserControl A and calls the Update-Method that UserControl B provides:

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

The advantage of this event-approach is that UserControls stay being reusable. You can use UserControl A in other pages as well even when they don't handle this event. It's part of the controller to decide what is needed and what should be done.

UserControls as a rule should not depend on specific controllers, otherwise they are hard-linked and not reusable. That would be also a good source for nasty erros. A UserControl might 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 -> the controller-UserControl adopts the page-role(see above)

Upvotes: 6

Related Questions