Event handler is always null

I searched intensively for this, but cannot find a solution to my problem. I am trying to call a function in the code behind the page from a user control on this page.

I have a web application that uses the homepage. I am adding a user control that I wrote on one of the content pages. I added a user control to an aspx page by dragging it from the toolbar. I can see the user control from the code behind, but I cannot access the public functions. To fix this problem, I created the user control object in the code behind and used the LoadControl function. It all works fine.

The problem I am facing is when I try to connect an EventHandler to an aspx page with a user control. Everything compiles and works fine, but I can't see anything on the page. I think the problem is that EventHandler is always null.

Custom control code

public partial class ucBuyerList : System.Web.UI.UserControl { public delegate void BuyerSelectedEventHandler(object sender, EventArgs e); public event BuyerSelectedEventHandler BuyerSelected; private string name = ""; public string Name { get { return name; } set { name = value; } } private string auid = ""; public string AUID { get { return auid; } set { auid = value; } } protected void Page_Load(object sender, EventArgs e) { } private void OnBuyerSelected(EventArgs e) { if (BuyerSelected != null) { BuyerSelected(this, new EventArgs()); } } protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e) { SetNameAndAUID(); OnBuyerSelected(e); } private void SetNameAndAUID() { name = lbBuyerList.SelectedItem.Text; auid = lbBuyerList.SelectedItem.Value; } } 

Parent Code

  public partial class frmBuyerInformation : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Master.changePageTitle("Buyer Information"); buyerList.BuyerSelected += new ucBuyerList.BuyerSelectedEventHandler(buyerListControl_BuyerSelected); } void buyerListControl_BuyerSelected(object sender, EventArgs e) { DisplayBuyerInformation(); } public void DisplayBuyerInformation() { tbName.Text = buyerList.Name; tbAUID.Text = buyerList.AUID; } } 

Can anyone see what I'm doing wrong?

EDIT: This issue is resolved. The above code snippets now function. If someone is facing the problem I had, you can simulate the code above. Make sure AutoEventWireup="true" on aspx and ascx pages. Thank you June Paik for your decision. Thanks Diego De Vita for your input.

+6
source share
2 answers

I also struggled with events for a long time. Currently, I always create them this way, because this is the only way I know that this works. Did not test it with your code, but here anyway:

 public partial class ucBuyerList : System.Web.UI.UserControl { public delegate void BuyerSelectedEventHandler(object sender, EventArgs e); public event BuyerSelectedEventHandler BuyerSelected; public string Name; public string AUID; protected void Page_Load(object sender, EventArgs e) { //Select the first buyer in the list when the user control loads if (!IsPostBack) { lbBuyerList.SelectedIndex = 0; } } private void OnBuyerSelected(EventArgs e) { BuyerSelectedEventHandler handler = BuyerSelected; if (handler != null) { handler(this, new EventArgs()); } } protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e) { Name = lbBuyerList.SelectedItem.Text; AUID = lbBuyerList.SelectedItem.Value; OnBuyerSelected(e); } } 

On the parent page, you can simply call your function the same way you do it.

+2
source

It is possible that Page_Load too late in the page life cycle to use LoadControl and subscribe to this event. What happens if you move this code to the Page_Init method?

+1
source

Source: https://habr.com/ru/post/921717/


All Articles