How can i call method from usercontrol from another usercontrol using asp.net?
Asked Answered
B

6

5

I have two usercontrols: UserControl1 and UserControl2, these controls are added in the Page.aspx.

UserControl1: This usercontrol contains method for hidden textboxes in this usercontrol. This method is called "HideTextbox"

UserControl2: I want to call method "HideTextBox" from UserControl2.

How can I call method HideTextBox from UserControl2 ?

Bekki answered 17/1, 2013 at 19:27 Comment(2)
Is UserControl2 a child control inside UserControl1?Mitchell
No. Usercontrol 2 is at the same level like UserControl1 - in Page.aspx.Bekki
C
6

Only if both are usercontrols or servercontrols, or you're looking for a servercontrol from a usercontrol will this work. (not from a servercontrol because you can't get a reference to the asp.usercontrol_xx assembly)

First get a reference to the parent page (usually this can be done with this.Parent. Next do a recursive FindControl on the parent to find a control whose type is UserControl2. Example code:

//this for extension method
public static UserControl2 FindUserControl2Recursive(this Control root)
{
    var uc2 = root as ASP.UserControls_UserControl2_ascx;
    if (uc2 != null)
    {
        return uc2;
    }
    foreach (var control in root.Controls)
    {
        uc2 = control.FindUserControl2Recursive();
        if (uc2 != null)
        {
            return uc2;
        }
    }
    return null;
}

Once you have your Usercontrol2 reference, you can easily call your public method.

Cussed answered 17/1, 2013 at 19:57 Comment(0)
C
6

This problem can be solved by creating a custom event in UC2 and consuming that event on the main page to invoke the hide method on UC1.

You declare a delegate in your user control

public delegate void HideTextBoxEventHandler(object sender, EventArgs e);

Then define the event for the delegate you created

public event HideTextBoxEventHandler HideTextBox;

At the point in the code where you want to hide text box you need to invoke that event:

if (this.HideTextBox != null) // if there is no event handler then it will be null and invoking it will throw an error.
{
   EventArgs e = new EventArgs();
   this.HideTextBox(this, e);
}

Then from the main page create an event handling method

protected void UserControl2_HideTextBox(Object sender, EventArgs e)
{
   UC1.InvokeHideTextBox();  // this is the code in UC1 that does the hiding
}

You will need to add to the page load or where ever you are loading UC2

UC2.HideTextBox += new UserControl2.HideTextBoxEventHandler(this.UserControl2_HideTextBox);
Currey answered 17/1, 2013 at 20:29 Comment(0)
E
1

I would declare interface on control that knows how to hide text boxes, something like this :

public interface ITextBoxHider
{
  void HideTextBoxes();
}

after that implement this interface on UserControl1, when you want to hide text boxes enumerate all controls on a form and find one that implements ITextBoxHider, and then simply call that method :

helper method that will enumerate all controls :

public static IEnumerable<Control> GetAllChildren(Control parent)
{
  foreach (Control control in parent.Controls)
  {
    foreach (Control grandChild in GetAllChildren(control))
        yield return grandChild;

    yield return control;
  }
}

using that method and calling HideTextBoxes from UserControl2 :

var hider = GetAllChildren(this.Page).FirstOrDefault(ct => ct is ITextBoxHider);
if (hider != null)
  (hider as ITextBoxHider).HideTextBoxes();
Effector answered 17/1, 2013 at 21:4 Comment(0)
S
0

On user control 2

UC1Class UserControl = Parent.FindControl("UC1_Name") as UC1Class;
UserControl.HideTextbox();

or shorten

(Parent.FindControl("UC1_Name") as UC1Class).HideTextbox();

On user control 1

public void HideTextbox()
{
    ....Do Something
}
Subclass answered 8/10, 2017 at 23:37 Comment(0)
M
-1

First you need to make HideTextBox() a public method, not private.

Then call UserControl1.HideTextBox().

Update: I wasn't clear about getting the reference to UserControl1. When I did this in my project, I created an interface that declared the method I was going to call, so my code was like this:

IUserControl1 uc1 = (IUserControl1)this.Parent.FindControl("ucOne");
uc1.HideTextBox();

Make sure your UserControl1 derives from the interface: public partial class UserControl1 : System.Web.UI.UserControl, IUserControl1

Mitchell answered 17/1, 2013 at 19:29 Comment(2)
True, but I think the real problem is getting a reference to UserControl2 from UserControl1.Stalker
Robert you're right. This error "Object reference not set to an instance of an object"Bekki
B
-1

If you want to access method using object of Usercontrol, you have to register it like this..

UserControl1 uc = new UserControl1();
uc.HideTextBox();

You have to declare HideTextBox() as public.

Also, you need to add a directive on a webform telling the webform that you are going to dynamically load the usercontrol. So in the webform, add the following directive.

<%@ Reference Control = "WebUserControl1.ascx" %>

Hope this helps

Bethought answered 17/1, 2013 at 19:35 Comment(1)
UC1 and UC2 both exist on a page but do not know about either other. Calling the method as you describe will call the method in a new instance of UC1 not the instance on the page.Currey

© 2022 - 2024 — McMap. All rights reserved.