Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 779
Jenan Avatar asked Jan 17 '13 19:01

Jenan


People also ask

What is ASCX in asp net?

ascx file, also known as a user control, requested from a server that hosts an ASP.NET Web application. The file must be called from a Web Forms page or a parser error will occur.

What is UserControl in asp net?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.


3 Answers

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.

like image 177
Destrictor Avatar answered Nov 01 '22 07:11

Destrictor


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);
like image 24
Chad Avatar answered Nov 01 '22 09:11

Chad


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();
like image 1
Antonio Bakula Avatar answered Nov 01 '22 07:11

Antonio Bakula