查找页面中用户控件中存在的控件,包括母版页 [英] Find controls present in user control in a page, that includes master page

查看:82
本文介绍了查找页面中用户控件中存在的控件,包括母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法访问包含母版页的页面中的用户控件中的控件。



我有一个主页A.Master

我有一个包含母版页的A.aspx页面

在A.aspx中我有一个名为Save&的按钮。一些用户控件,如myusercontrol1,myusercontrol2等...



当我点击保存按钮时,我需要访问该用户控件中的控件。得到的价值。



我尝试过:



i在web.config文件中注册了这些用​​户控件。

i解除了保存按钮&的点击事件。编写以下代码

myusercontrol1 obj = new myusercontrol1()

Gridview grvuserdetails =(Gdidview)obj.findcontrol(GridView1)。



但是价值在obj& grvuserdetails变为null。

解决方案

查找控件| ASP.NET论坛 [ ^ ]


HI Arya

如果你试图在<$ c $中找到控件 c>空对象(新实例)然后您将只获得 null

  myusercontrol1 obj =  new  myusercontrol1() 



而不是,使用用户控件名称(在 aspx 文件中定义)

例如:if控件名称是 userControl1 然后代码就像

 Gridview grvuserdetails =(Gdidview)useControl1.findcontrol(   GridView1


 ContentPlaceHolder CP =(ContentPlaceHolder) this  .Page.Master.FindControl(  ContentPlaceHolder1); 
UserControl userControl =(UserControl)CP.FindControl( useControl1);
Gridview grvuserdetails =(Gridview)userControl.FindControl( GridView1);


I cant't access the Controls Present In A User Control In A Page Which Includes Master Page.

I Have A Masterpage A.Master
I Have A Page A.aspx which include masterpage
In A.aspx i have a button named Save & Some Usercontrols like myusercontrol1,myusercontrol2 etc...

I need that when i click the save button i want to access the controls present in that user controls & get the value present in.

What I have tried:

i registered those usercontrols in web.config file.
i fired the click event of save button & write the following code
myusercontrol1 obj = new myusercontrol1()
Gridview grvuserdetails=(Gdidview)obj.findcontrol("GridView1").

But The Value In obj & grvuserdetails becomes null.

解决方案

Finding controls | The ASP.NET Forums[^]


HI Arya
if you try to find the control in an empty object (new instance) then you will get null only.

myusercontrol1 obj = new myusercontrol1()


instead, use the user control Name (which is defined in the aspx file )
eg: if the control name is userControl1 then the code wil be like

Gridview grvuserdetails=(Gdidview)useControl1.findcontrol("GridView1")


ContentPlaceHolder CP = (ContentPlaceHolder)this.Page.Master.FindControl("ContentPlaceHolder1");
UserControl userControl= (UserControl )CP.FindControl("useControl1");
Gridview grvuserdetails= (Gridview)userControl.FindControl("GridView1");


这篇关于查找页面中用户控件中存在的控件,包括母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆