How to call a method from user control to aspx page?
15,067
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 += ...
Related videos on Youtube
Author by
user2500094
Updated on September 23, 2022Comments
-
user2500094 over 1 year
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
-
user2500094 over 10 yearsThanks for your response then how to call that event(UserButtonClicked)