1

I have an user control page i.e. template(.ascx) where in I have a repeater which gets loaded by executing a stored procedure. There is also a parent page which has its own repeater along with a button on each row and this repeater too gets loaded with the execution of the stored procedure.

Now, the button on each row of the parent repeater, when that gets clicked first two column values of that particular rows (Eg: G6300 and Group Term Ife Insurance) needs to be passed to the stored procedure present in the template where the child repeater exists and child repeater should be visible.

Till now, I am successful in displaying the parent repeater, passing that particular row values on the button click to the child stored procedure and also successful in getting the results in list by executing the child stored procedure. But when I try to assign this list to the child repeater present on template page it gives an exception for the child repeater (System.NullReferenceException: Object reference not set to an instance of an object).

I am guessing I am missing initializing the child repeater and hence it is null. But not completely positive about it and also about the solution.

Please suggests something.

//Template code (Inner Repeater)

        protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack)
        {
            return;
        }

    }

    public void LoadRepeater(string id, string policyNo, string desc) //gets called from parent repeater
    {
        //Loading the tempalte
        StringBuilder template1 = MembershipRepository.LoadPolicyDetailsTemplate("PolicyDetailsTemplate1.ascx");

        CustomerPortalWebContentEntities context = new CustomerPortalWebContentEntities();
        RetrievePolicyDetails policyDetails = new RetrievePolicyDetails();
        List<PolicyDetails> polDetails = policyDetails.GetPolicyDetails(id, policyNo, desc); //polDetails HAS ALL THE REQUIRED ROWS


        rptPolicyDetails.DataSource = polDetails; //EXCEPTION ON THIS LINE
        rptPolicyDetails.DataBind();




    }

//Button click event for each row on Parent page(parent repeater), where the template/child repeater is getting called

protected void rptCoverage_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "CoverageDetails")
        {
            //Button was clicked
            var policy = e.Item.FindControl("lblPolicyNo") as Label;
            var description = e.Item.FindControl("lblDescription") as Label;
            var idPerson = id_person;

            var placeHolder = e.Item.FindControl("UCPlaceHolder") as Control;

            placeHolder.Controls.Add((PolicyDetailsTemplate1)Page.LoadControl("~/Shared/Controls/PolicyDetailsTemplate1.ascx")); 

            //this.placeHolder.Controls.Add((PolicyDetailsTemplate1)Page.LoadControl("~/Shared/Controls/PolicyDetailsTemplate1.ascx")); 

            //calling the specific tempalte with specific sp                                        /**/  will change w.r.t multiple templates
            PolicyDetailsTemplate1 template = new PolicyDetailsTemplate1();
            template.LoadRepeater(idPerson, policy.Text.Trim(), description.Text.Trim());



        }
    }

//Here is the snippet of the output enter image description here

Nalini
  • 213
  • 1
  • 3
  • 12

1 Answers1

0

You are binding data to a different instance of PolicyDetailsTemplate1 than what you are adding to your PlaceHolder.

Here you add one PolicyDetailsTemplate1 to the PlaceHolder:

placeHolder.Controls.Add((PolicyDetailsTemplate1)Page.LoadControl("~/Shared/Controls/PolicyDetailsTemplate1.ascx")); 

But then you create a whole new instance:

//calling the specific tempalte with specific sp
PolicyDetailsTemplate1 template = new PolicyDetailsTemplate1();
template.LoadRepeater(idPerson, policy.Text.Trim(), description.Text.Trim());

Instead you could try just creating one PolicyDetailsTemplate1, binding its Repeater, then adding it to the PlaceHolder:

PolicyDetailsTemplate1 template = (PolicyDetailsTemplate1)Page.LoadControl("~/Shared/Controls/PolicyDetailsTemplate1.ascx);
template.LoadRepeater(idPerson, policy.Text.Trim(), description.Text.Trim());
placeHolder.Controls.Add(template);
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • Thank you BubbleHearth for your reply. But rptPolicyDetails repeater is declared in the template (child repeater), so how can I do the find control in the parent page? And I also tried doing it but it returned the same exception. – Nalini Jul 14 '14 at 21:43
  • Ok, I may have misread your question. So each child repeater is contained within it's own user control? And the parent repeater contains many different child user controls? – j.f. Jul 14 '14 at 22:01
  • Yes Child repeater (template - .ascx) has its own user control and Parent repeater just contain one repeater which as per the output image will have two rows with policyNo G6300 and G-14798-2. Which in turn will call the child repeater. I know its confusing, sorry. – Nalini Jul 14 '14 at 22:14
  • Ok, that makes sense. See my edit. I think that should at least get us on the right track. – j.f. Jul 14 '14 at 23:00