事件处理程序总是空 [英] Event Handler is Always null

查看:148
本文介绍了事件处理程序总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经广泛地搜索了这一点,但无法找到解决我的问题。我试图从该网页上的用户控件调用中的code函数的页面背后。

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

我有一个使用母版页的Web应用程序。我补充说,我写的内容网页之一的用户控件。我通过拖动并从工具箱中添加它的用户控件的aspx页面。我能看到从code后面的用户控件,但我不能访问公共职能。为了解决这个问题,我创建了后面的code用户控制的对象,并使用LoadControl功能。所有这一切似乎很好地工作。

I have a web application that uses a master page. I am adding a user control that I wrote to one of the content pages. I added the user control to the aspx page by dragging and dropping it from the toolbox. I am able to see the user control from the code behind, but I cannot access the public functions. To fix that problem, I created an object of the user control in the code behind and used the LoadControl function. All of that seems to work fine.

我遇到的问题是,当我试图钩到从aspx页面给用户控件的事件处理程序。一切编译和运行得很好,但我没有看到任何事情发生在页面上。我认为这个问题是该事件处理程序总是空。

The problem I am having is when I am trying to hook the into the EventHandler from the aspx page to the user control. Everything compiles and runs just fine, but I am not seeing anything happen on the page. I think the issue is that the EventHandler is always null.

用户控制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;
    }
}

父页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 am doing wrong?

编辑:此问题已得到解决。上面的code snippits现功能。如果有人跑进我有问题,可以建模code以上。确保 AutoEventWireup =真在ASPX和ASCX页面中。谢谢白先生月您的解决方案。谢谢圣地亚哥德维塔您的输入,以及。

This issue has been resolved. The code snippits above are now functional. If anyone runs into the issue I had, you can model the code above. Make sure that AutoEventWireup="true" in both the aspx and ascx pages. Thank you June Paik for your solution. Thank you Diego De Vita for your input as well.

推荐答案

我一直在挣扎的事件相当一段时间为好。现在,我总是创建它们通过这种方式的原因,而是我知道它的唯一途径。有没有与你的code测试,但在这里不言而喻反正:

I've been struggling with events for quite a while as well. Nowadays I always create them this way 'cause it's the only way I know it works. Haven't tested it with your code but here it goes 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);
    }
}

在父页面你可以打电话给你的功能,你已经在做了同样的方式。

In the parent page you can just call your function the same way you're doing it already.

这篇关于事件处理程序总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆