ASP.NET LoadControl 使用关键字“base”; 在此上下文中无效

发布于 2024-07-25 00:40:10 字数 672 浏览 2 评论 0原文

我想要一个 Web 服务来加载 .ascx 控件,在其中加载一些值,然后返回该控件的 HTML 内容。 我有类似的东西:

[WebMethod(EnableSession = true)]
public void GetHTML()
{
    UserControl loader = new UserControl();
    MyCustomReport reportControl =
        (MyCustomReport)loader.LoadControl("~/The/path/to/the/.ascx");
    reportControl.DataBind();

    return "TODO";
}

MyCustomReport 覆盖 DataBind()

public override void DataBind()
{
    base.DataBind();

    // etc.
}

base.DataBind() 抛出 NullReferenceException 并且调试器显示:

在此上下文中使用关键字“base”无效

任何帮助将不胜感激,谢谢!

I want a web service to load an .ascx control, load some values in it and then return the HTML content of this control. I have something like that:

[WebMethod(EnableSession = true)]
public void GetHTML()
{
    UserControl loader = new UserControl();
    MyCustomReport reportControl =
        (MyCustomReport)loader.LoadControl("~/The/path/to/the/.ascx");
    reportControl.DataBind();

    return "TODO";
}

MyCustomReport overrides DataBind():

public override void DataBind()
{
    base.DataBind();

    // etc.
}

The row base.DataBind() throws a NullReferenceException and the debugger says:

Use of keyword 'base' is not valid in this context

Any help will be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-08-01 00:40:10

试试这个:

public override void OnDataBinding() 
{ 
    base.OnDataBinding(); 

    // other stuff here ...
}

UserControl< /a> 没有虚拟 DataBind 方法,但有虚拟 OnDataBinding 方法。 我相信这是您想要重写的方法。

Try this:

public override void OnDataBinding() 
{ 
    base.OnDataBinding(); 

    // other stuff here ...
}

UserControl does not have a virtual DataBind method but it does have a virtual OnDataBinding method. I believe this is the method you mean to override.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文