views:

368

answers:

1

Hi, 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!

+1  A: 

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.

Andrew Hare
10x Andrew, this way it does not crash at base.OnDataBinding(); however I think this changes the meaning of the code. MyCustomReport has subcontrols, with a similar override - before, the base.DataBind(); led to their databinding, now their OnDataBinding()s don't get called.The same control works, when put directly into an .aspx file, the problem seems to be the way I load it in the Web service, yet I cannot figure what out what is wrong exactly.