1
votes

I am new to user controls, I have a situation which i am unable to solve like below

When clicked on a button in user control1 bind a drop down in user control2 on the same page. and user controls are in their own update panels.

UserControl1 ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="SampleWebApp.WebUserControl1" %>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

UserControl2 ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SampleWebApp.WebUserControl2" %>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Above two user controls are in a aspx page like below aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
     <div>
    <UC2:UserControl1 ID="UserControl1" runat="server" />
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
     <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
     <div>
<UC2:UserControl2 ID="usercontrol2" runat="server" />
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>

Now Big Question is when i click button in user control 2 then drop down list in user control 1 shud b bound Please help

Thanks in advance

1

1 Answers

0
votes

I recommend setting up events in each of the user controls that the page hosting the user controls subscribes to and then there is logic in the page event handlers that knows to tell the user control via methods/properties in the user control that they need to rebind, like this:

public class UserControlClass1
{
    // Define event that will be raised by user control to anyone interested in handling the event
    public event UC_Button1ClickEventHandler UC_Button1Click;
    public delegate void UC_Button1ClickEventHandler();

    public void DoSomething1()
    {
       // This method can do anything you want the control to do (i.e. rebind, etc.)
    }

    // Mechanism to allow event to be raised by user control
    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        if (UC_Button1Click != null) 
        {
            UC_Button1Click();
        }
    }
}

public class UserControlClass2
{
    // Define event that will be raised by user control to anyone interested in handling the event
    public event UC_Button2ClickEventHandler UC_Button2Click;
    public delegate void UC_Button2ClickEventHandler();

    public void DoSomething2()
    {
       // This method can do anything you want the control to do (i.e. rebind, etc.)
    }

    // Mechanism to allow event to be raised by user control
    private void Button2_Click(System.Object sender, System.EventArgs e)
    {
        if (UC_Button2Click != null) 
        {
            UC_Button2Click();
        }
    }
}

Now in the .aspx page that is hosting both of the user controls, it can subscribe to each of the user control events, like this:

userControl1.UC_Button1Click += Button1_Click;
userControl2.UC_Button2Click += Button2_Click;

And here in the implementation of the event handler is where the page manages the interaction between the two user controls, like this:

// When the button in user control 1 is clicked, then the DoSomething2 method
// is called in user control 2 (i.e. bind data, etc.)
public void Button1_Click(object sender, EventArgs args)
{
    // Call the DoSometing2 method in user control 2
    userControl2.DoSomething2();
}

// When the button in user control 2 is clicked, then the DoSomething1 method
// is called in user control 1 (i.e. bind data, etc.)
public void Button2_Click(object sender, EventArgs args)
{
    // Call the DoSometing1 method in user control 1
    userControl1.DoSomething1();
}

In short, the page handles the references to the user controls, so the two user controls do not each other are both on the page or that the each of other even exist. The page also handles coordinating what happens when an event is raised by either user control and calling into the other control via a public method.