How can I call a method from usercontrol from another usercontrol using asp.net?

I have two user controls: UserControl1 and UserControl2 , these controls are added to page.aspx.

UserControl1 : This usercontrol contains the hidden text field method in this usercontrol. This method is called " HideTextbox "

UserControl2 : I want to call the " HideTextbox " method from UserControl2 .

How can I call the HideTextbox method from UserControl2 ?

+4
source share
6 answers

Only if both are users or a server, or you are looking for server control from usercontrol, will this work. (not from servercontrol because you cannot get the assembly reference asp.usercontrol_xx)

First get a link to the parent page (this can usually be done using this.Parent . Then do a recursive FindControl for the parent to find a control of type UserControl2 . Code example:

 //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 the Usercontrol2 link, you can easily call your public method.

+5
source

This problem can be solved by creating a custom event in UC2 and using this event on the main page to call the hide method in UC1.

You declare a delegate in your user control

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

Then define an event for the delegate you created.

 public event HideTextBoxEventHandler HideTextBox; 

At the code point where you want to hide the text box, you need to trigger this 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 ever load UC2

 UC2.HideTextBox += new UserControl2.HideTextBoxEventHandler(this.UserControl2_HideTextBox); 
+4
source

I would declare a management interface that knows how to hide text fields, something like this:

 public interface ITextBoxHider { void HideTextBoxes(); } 

after that, implement this interface in UserControl1, when you want to hide text fields, list all the controls on the form and find the one that implements ITextBoxHider, and then just call this method:

helper method that will list all the 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 this method and calling HideTextBoxes from UserControl2:

 var hider = GetAllChildren(this.Page).FirstOrDefault(ct => ct is ITextBoxHider); if (hider != null) (hider as ITextBoxHider).HideTextBoxes(); 
+1
source

In user control 2

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

or cut

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

In user management 1

 public void HideTextbox() { ....Do Something } 
0
source

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

Then call UserControl1.HideTextBox() .

Update: I did not know how to get a link to UserControl1. When I did this in my project, I created an interface that declared a method that I was going to call, so my code was something like this:

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

Make sure that UserControl1 is obtained from the interface: public partial class UserControl1: System.Web.UI.UserControl, IUserControl1

-1
source

If you want to access the method using the Usercontrol object, you need to register it as follows.

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

You must declare HideTextBox () public.

In addition, you need to add a directive to the web form telling the web form that you are going to dynamically load usercontrol. Therefore, in the web form, add the following directive.

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

Hope this helps

-1
source

All Articles