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

亡梦爱人 提交于 2020-01-04 04:03:16

问题


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


回答1:


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 += ...



回答2:


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();
    }



回答3:


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);
    



回答4:


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


来源:https://stackoverflow.com/questions/19027066/how-to-call-a-method-from-user-control-to-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!