Monday, April 29, 2013

Access UserControl methods from aspx page


A few days back I was facing a problem where I had to access value of an ASCX Control’s controls and also its public method from another ASCX Control while both are in same ASPX page.

The first scenario was like this: UserControl_1 has a DropDownList and UserControl_2 has a Label. When theSelectIndexChange is being raised from UserControl_1 I need to set SelectedItem’s value into the Label ofUserControl_2.

And in second scenario the UserControl_2 will need to invoke a public method which is in UserControl_1.

I did some googling and I found out using FindControl property would be the simpler way to solve the first scenario.

And as for the second scenario there are couple of good solutions out there. Like using Reflection or using Interface. Here I will cover it using Interface.

WebUserControl1.ascx
<%@ Control Language="”C#”" AutoEventWireup=”true” CodeFile=”WebUserControl1.ascx.cs” Inherits=”WebUserControl1″ %>
<p>
    <b>WebUserControl 1</b></p>
    <asp:DropDownList ID="”DropDownList1″" runat="server" 
          AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>Red</asp:ListItem>
        <asp:ListItem>Blue</asp:ListItem>
        <asp:ListItem>Green</asp:ListItem>
    </asp:DropDownList>

WebUserControl2.ascx
<%@ Control Language="”C#”" AutoEventWireup=”true” CodeFile=”WebUserControl2.ascx.cs” Inherits=”WebUserControl2″ %>
<p><b>WebUserControl 2</b></p>
<asp:Label ID=”Label1″ runat="”server”" Text=”Label”></asp:Label>
<asp:Button ID=”Button1″ runat="”server”" onclick=”Button1_Click” Text=”Button” />

WebUserControl1.ascx.cs (Code Behind of WebUserControl1.ascx)

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
     ((Label)((this.Parent).FindControl(“WebUserControl21″)).FindControl(
           “Label1″)).Text = this.DropDownList1.SelectedItem.Text;
}

This will do the job for the first scenario. The FindControl property takes an id as a parameter and also you will need to cast the Control which you are about to work with.
As for invoking a public method triggering from one to another will need to help of ASPX page who is containing the both ASCX.
Suppose we have this public method on WebUserControl1.ascx.cs

public string GetTheText() {
    return “This text is from WebUserControl 1″;
}

Now we need to call this method from WebUserControl2. First Create an Interface.
IMyInterface.cs

public interface IMyInterface {
   string GetMethod1();
}

Second implement this interface in your ASPX page where ‘GetMethod1()’ is only a via for WebUserControl2triggering the public method of WebUserControl1.

Page1.aspx
<div id = “wuc1″ runat ="”server”" style=”width: 440px”>
  <uc1:WebUserControl1 ID=”WebUserControl11″ runat="”server”" />
</div>
<div id = “wuc2″ runat ="”server”" style=”height: 23px; width: 441px”>
  <uc2:WebUserControl2 ID=”WebUserControl21″ runat="”server”" />
</div>

Page1.aspx.cs (Code Behind)
public partial class WPage : System.Web.UI.Page, IMyInterface {
protected void Page_Load(object sender, EventArgs e) {
   }
public string GetMethod1() {
  return this.WebUserControl11.GetTheText();
   }
}

Last, on a Button_Click event of WebUserControl2 you can invoke the Page’s method which was implemented before is basically calling the WebUserControl1′s desired public method.
WebUserControl2.ascx.cs (Code Behind of WebUserControl2.ascx)

public partial class WebUserControl2 : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {
       }
    protected void Button1_Click(object sender, EventArgs e) {
      IMyInterface iMI = this.Page as IMyInterface;
      if (iMI != null)
        Label1.Text = iMI.GetMethod1();sp; Label1.Text = iMI.GetMethod1();
       }
   }

Another solution is to make a base class and make a virtual method. You can then use this as a base for your page and override the method. The ASCX has then no problem calling via the base either. I find this solution handy too.

No comments:

Post a Comment