简体   繁体   中英

How to call a method from user control to aspx page?

I want to call a method from user control to aspx page

I'm trying but I am not able to call that method in aspx page

Code:

AddVisaUserControl.ascx.cs

public event EventHandler UserControlButtonClicked;

    public void OnUserControlButtonClick()
    {
        if (UserControlButtonClicked != null)
        {
            UserControlButtonClicked(this,EventArgs.Empty);
        }
    }

protected void btnRemove_Click(object sender, EventArgs e)
    {
        OnUserControlButtonClick();
    }

.aspx

Edit

In the below code when the page load I am getting "null reference error"

AddVisaControl av; 
protected void Page_Load(object sender, EventArgs e)
    {
        av.UserControlButtonClicked  += new
                EventHandler(AddVisaUserControl_UserControlButtonClicked);
    } 

  private void AddVisaControl_UserControlButtonClicked(object sender, EventArgs e)
    {
        var ctrl = (AddVisaControl)LoadControl(@"AddVisaControl.ascx");
        //ctrl.ID = i;
        this.rpt1.Controls.Remove(ctrl);
    }

Any ideas? Thanks in advance

You appear to have an uninitialized field:

AddVisaControl av; 

whose default value is null, hence the NullReferenceException.

If you have added an instance of the UserControl to your aspx page, you should have an instance whose name is equal to the ID of the UserControl instance:

=== in Page.aspx

<uc1:AddVisaUserControl ID="MyControl" ... />

=== in Page.aspx.cs

MyControl.UserControlButtonClicked += ...

Your ascx control

public delegate void ButtonClickEventHandler(string data);
public event ButtonClickEventHandler ButtonClickEvent = null;

if (ButtonClickEvent != null)
            ButtonClickEvent("Send to aspx");

Your aspx page :

<%@ Register TagPrefix="uc" TagName="uc1" 
             Src="~/Controls/AddVisaUserControl.ascx" %>
<uc:AddVisaControl id="uc1" runat="server" />


 protected void Page_Load(object sender, EventArgs e)
    {
        uc1.ButtonClickEvent += new yourusercontrol.ButtonClickEventHandler(Login1_ButtonClickEvent);
    }

    void uc1_ButtonClickEvent(string data)
    {
        lbldefaultaspx.Text = data.ToString();
    }

You have created the delegate via the += new syntax, but you do not have the actual method that gets invoked in your page code.

In other words, you need a AddVisaUserControl_UserControlButtonClicked method in your .aspx page code, like this:

protected void AddVisaUserControl_UserControlButtonClicked(object sender, 
                                                           EventArgs e)
{
    // Logic here for what the page does when the user control's remove 
    // button is clicked

}

UPDATE:

Upon the OP posting more code, it seems the user control is a null reference, because of this line:

AddVisaControl av;

This does not instantiate the user control, so you can do two things:

  1. Instantiate the user control, like this:

     AddVisaControl av = new AddVisaControl(); 

    Invoke the delegate, like this:

     av.UserControlButtonClicked += new EventHandler(AddVisaUserControl_UserControlButtonClicked); 
  2. Add the user control to the markup of your page, like this:

     <%@ Register TagPrefix="uc" TagName="AddVisaControl" Src="~/Controls/AddVisaUserControl.ascx" %> <uc:AddVisaControl id="AddVisaControl1" runat="server" /> 

    Invoke the delegate, like this:

     AddVisaControl1.UserControlButtonClicked += new EventHandler(AddVisaUserControl_UserControlButtonClicked); 

Usercontrol.ascx

Me.Page.GetType.InvokeMember("ClosePopUp", System.Reflection.BindingFlags.InvokeMethod, Nothing, Me.Page, New Object() {parameter1,parameter2})

ParentPage.aspx

Public Sub ClosePopUp(parameter1,parameter2)
/*Your Logic Here
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM