第一种:在用户控件页面放一隐藏域或者控件(隐藏掉),在父页面给用户控件的控件赋值
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
HiddenField hiddenFieldPemission
=
AssetQuery1.FindControl(
"
HiddenFieldPemission
"
)
as
HiddenField;
2 hiddenFieldPemission.Value = g_Permission;
3 HiddenField hiddenFieldEmail = AssetQuery1.FindControl( " HiddenFieldEmail " ) as HiddenField;
4 hiddenFieldEmail.Value = g_Mail;
2 hiddenFieldPemission.Value = g_Permission;
3 HiddenField hiddenFieldEmail = AssetQuery1.FindControl( " HiddenFieldEmail " ) as HiddenField;
4 hiddenFieldEmail.Value = g_Mail;
第二种:先在用户控件页面定义一个变量
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
public
partial
class
AssetQuery : System.Web.UI.UserControl
2 {
3 public string str;
4 protected void Page_Load( object sender, EventArgs e)
5 {
6 Label1.Text = str;
7 }
8 }
2 {
3 public string str;
4 protected void Page_Load( object sender, EventArgs e)
5 {
6 Label1.Text = str;
7 }
8 }
在父页面中:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
//
protected System.Web.UI.WebControls.PlaceHolder phTestCtl;
2
3 AssetQuery uc = (AssetQuery)Page.LoadControl( " AssetQuery.ascx " );
4 uc.str = g_Permission;
5 Page.Controls.Add(uc);
2
3 AssetQuery uc = (AssetQuery)Page.LoadControl( " AssetQuery.ascx " );
4 uc.str = g_Permission;
5 Page.Controls.Add(uc);
需要注意的是,如果用户控件页面中包含scriptManager,add时会有问题,这时你可以把scriptManager移到父页面,
用户控件页面则换成ScriptManagerProxy,因为页面只能包含一个scriptManager;
第三种:反射,把父页面的赋值放到一个方法里
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
public
Result
Load()
2 {
3 Result r = new Result();
4 web_AssetQuery uc = (web_AssetQuery)Page.LoadControl( " AssetQuery.ascx " );
5 r.G_email = g_Mail;
6 r.G_permission = g_Permission;
7 return r;
8 }
2 {
3 Result r = new Result();
4 web_AssetQuery uc = (web_AssetQuery)Page.LoadControl( " AssetQuery.ascx " );
5 r.G_email = g_Mail;
6 r.G_permission = g_Permission;
7 return r;
8 }
然后在用户控件页面调用这个方法:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
//
获得父页面
2 Page p = this .Parent.Page;
3 Type pageType = p.GetType();
4 // 父页面的方法名
5 System.Reflection.MethodInfo mi = pageType.GetMethod( " Load " );
6 // 执行
7 object o = mi.Invoke(p, new object [] { }) ; // 父页面方法若带参数则依次写上
8 g_Permission = o.G_Permission;
9 g_Mail = o.G_Mail ;
2 Page p = this .Parent.Page;
3 Type pageType = p.GetType();
4 // 父页面的方法名
5 System.Reflection.MethodInfo mi = pageType.GetMethod( " Load " );
6 // 执行
7 object o = mi.Invoke(p, new object [] { }) ; // 父页面方法若带参数则依次写上
8 g_Permission = o.G_Permission;
9 g_Mail = o.G_Mail ;
对于这个取值,其实后面两种方法有点杀鸡用牛刀了,我写出来只是在其他类似场合可以用上,比如子页面(弹出窗口,模态框)
与父页面等交互,也可以尝试用这些方式实现取值。