如何调用从用户控件aspx页面的方法? [英] How to call a method from user control to aspx page?

查看:129
本文介绍了如何调用从用户控件aspx页面的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话从用户控件的aspx页面的方法

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

我想,但我不能够打电话aspx页面的方法

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

code:

AddVisaUserControl.ascx.cs

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

修改

在低于code当页面加载我得到空引用错误

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; 

其默认值为空,因此NullReferenceException异常。

whose default value is null, hence the NullReferenceException.

如果您已经添加了用户控件的一个实例到你的ASPX页面,你应该有一个实例,其名称等于用户控件实例的ID:

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:

===在Page.aspx

=== in Page.aspx

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

===在Page.aspx.cs

=== in Page.aspx.cs

MyControl.UserControlButtonClicked += ...

这篇关于如何调用从用户控件aspx页面的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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