0

I am using below link for dynamically calling Usercontrols based on my conditions in aspx page. Link which i am using to dynamically load User-Controls

Now the question is that,

1) I am creating the object of respective user control, to call the submit functionality of that page. (is this the correct way to do this).

2) In the child page, ie(User Control), I get Object Reference Exception, while accessing the controls of this user control.

Can anybody please help.

Hi, Enclosing sample code for further clarification. 1) user Control code that i am using.

namespace WebApplication1
{
    public partial class UserControlOne : System.Web.UI.UserControl
    {
       public void btnUserControlOne_Click(object sender, EventArgs e)
       {
            lblUserControlOne.Text = "User Control Button Clicked";
        }
     }
}

The error of object reference is coming at this line. lbl.Text.

2) The page where this user control is called is below.

namespace WebApplication1
{
   public partial class parentWebForm : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         System.Web.UI.UserControl uc = 
        (System.Web.UI.UserControl)Page.LoadControl("UserControlOne.ascx");

         divLoadControl.Controls.Add(uc);
       }

     protected void btnParent_Click(object sender, EventArgs e)
     {
        UserControlOne usrCntrlOne = new UserControlOne();
        usrCntrlOne.btnUserControlOne_Click(sender, e);
     }
   }
}
  • Without any code and exact explanation what’s happening there’s not much we can say. Please include a [mcve] or at least the appropriate parts of code, the functionality required, what happens, what should happen etc – Sami Kuhmonen Dec 05 '18 at 06:05
  • Hi @Sami Added the code for explaining further. – Anup Ksagar Dec 05 '18 at 07:26

1 Answers1

0

Solved this problem using below link Link of solution which uses delegate. So what i did is, i created a Delegate for the function, as denoted in the solution link above, and as i wanted to redirect to next page, on control returning to delegate event in parent page, this task was accomplished.

So, Basically i did the other way round, called the user-control event in content page, as the name of events in all the dynamic controls is same, so it solved the issue.