0

i'm trying to load a user control from codebehind service and get it's html value. However there's a repeater in that control that isn't getting loaded/initialized so it's always null

i get

Object reference not set to an instance of an object.

within the loadOrderInvoiceView function when it tries to

// fill repeater
this.rptView.DataSource = result;
this.rptView.DataBind();

at the line where it says this.rptView.DataSource = result;... i've tried to debug and for some reason this.rptView is null

And here's the service call

    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    //ctrl.RenderControl(hw);

    myControls.OrderInvoiceView oiv = new myControls.OrderInvoiceView();
    oiv.loadOrderInvoiceView(OID);// load the control with order id
    oiv.RenderControl(hw);

    return sb.ToString();

thanks in advance for any ideas. I'm using .net 4

3
  • How are you initializing this.rptView? Commented Dec 29, 2011 at 22:56
  • i tought making the control will automatically initialize it.. anyway i'll be answering my own question. i just found out how to get around it Commented Dec 29, 2011 at 23:03
  • possible duplicate of How do I get the HTML output of a UserControl in .NET (C#)? Commented Dec 29, 2011 at 23:16

1 Answer 1

3

i found out how to get around it

    Page p = new Page();
    myControls.OrderInvoiceView oiv = (myControls.OrderInvoiceView)p.LoadControl("~/myControls/OrderInvoiceView.ascx");

instead of

myControls.OrderInvoiceView oiv = new myControls.OrderInvoiceView();

final result

        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        Page p = new Page();
        myControls.OrderInvoiceView oiv = (myControls.OrderInvoiceView)p.LoadControl("~/myControls/OrderInvoiceView.ascx");

        //myControls.OrderInvoiceView oiv = new myControls.OrderInvoiceView();
        oiv.loadOrderInvoiceView(OID);
        oiv.RenderControl(hw);

        return sb.ToString();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.