简体   繁体   English

无法访问子页面中动态调用的UserControl中的控件

[英]Not able to access controls from Usercontrol called dynamically in child page

I am using below link for dynamically calling Usercontrols based on my conditions in aspx page. 我正在使用以下链接根据我在aspx页面中的条件动态调用Usercontrols。 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. 1)我正在创建各个用户控件的对象,以调用该页面的提交功能。 (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. 2)在子页面(即(用户控件))中,当访问该用户控件的控件时,出现对​​象引用异常。

Can anybody please help. 有人可以帮忙吗?

Hi, Enclosing sample code for further clarification. 嗨,随附示例代码以进一步说明。 1) user Control code that i am using. 1)我正在使用的用户控制代码。

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. lbl.Text。

2) The page where this user control is called is below. 2)调用此用户控件的页面如下。

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);
     }
   }
}

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. 所以我要做的是,如上面的解决方案链接中所述,我为该函数创建了一个Delegate,并且当我想重定向到下一页时,在控件返回到父页面的委托事件时,此任务已完成。

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. 因此,基本上,我做了另一种方法,即在内容页面中称为用户控件事件,因为所有动态控件中的事件名称都是相同的,因此它解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM