查找控制在母版返回nulll [英] Find Control on MasterPage returns nulll

查看:113
本文介绍了查找控制在母版返回nulll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去访问一个ModalPopupExtender控制,它是永诺返回null或对象集未设置为一个对象的intance。我试过master.Page.FindControl()和master.FindControl()和IM仍然没有得到结果。

 母版由主Page.Master为母版;
AjaxControlToolkit.ModalPopupExtender弹出= master.Page.FindControl(ModalPopupExtender2)作为AjaxControlToolkit.ModalPopupExtender;

更新时间:不能改变我的标签的文本母版页

 母版由主Page.Master;
AjaxControlToolkit.ModalPopupExtender popup1 = master.FindControl(ModalPopupExtender1)作为AjaxControlToolkit.ModalPopupExtender;
标签lblMessage = master.FindControl(lblMessage)作为标签;
lblMessage.Text =味精;
文字ltrlMessage = master.FindControl(ltrlMessage)作为文字;
ltrlMessage.Text =味精;标签MessageStatus = master.FindControl(lblMessageStatus)作为标签;
MessageStatus.Text = msgStatus;
popup1.Show();


解决方案

查看回答。你可以有一个强类型的母版页的,所以你不必找到并再抹上控制。在主控制将是公开访问,页面会知道母版页的类型,并将其访问。

编辑:


  

控制是不公开的。


假设你已经设置的主属性在你的页面指令:

 <%@页的MasterPageFile =〜/ MyMaster.master...

奇怪的是,你可能并不需要真正得到控制。相反,你需要设置在主网页的东西。我只是用一个内部方法,做你需要做的:

 公共部分类MyMaster:母版
{
    内部空隙SetTheFoo(串富)
    {
        this.WhateverControl.Text = foo的;
    }    //等等...
}

然后,从您的网页,只需要调用它:

  Master.SetTheFoo(富);

如果你仍然需要去控制,那么在你的母版页,您可以添加一个公共财产暴露你的模式弹出扩展。

 公共AjaxControlToolkit.ModalPopupExtender MyModalPopup
{
    {返回this.TheNonPublicModalPopupExtenderControl; }}

Im trying to access a ModalPopupExtender control and it allways is returning null or object set not set to an intance of an object. I've tried master.Page.FindControl("") and master.FindControl("") and im still not getting the result

MasterPage master = Page.Master as MasterPage;
AjaxControlToolkit.ModalPopupExtender popup = master.Page.FindControl("ModalPopupExtender2") as AjaxControlToolkit.ModalPopupExtender;

Updated: Cannot change the text of my labels in master page

MasterPage master = Page.Master;
AjaxControlToolkit.ModalPopupExtender popup1 = master.FindControl("ModalPopupExtender1") as AjaxControlToolkit.ModalPopupExtender;
Label lblMessage = master.FindControl("lblMessage") as Label;
lblMessage.Text = msg;
Literal ltrlMessage = master.FindControl("ltrlMessage") as Literal;
ltrlMessage.Text = msg;

Label MessageStatus = master.FindControl("lblMessageStatus") as Label;
MessageStatus.Text = msgStatus;        
popup1.Show();

解决方案

Check out this answer. You can have a strongly typed master page, so you don't have to find and then cast the control. The control on the master would be publicly accessible, and the page would know the type of the master page and have it accessible.

EDIT:

the control is not public

Assuming you've set the Master property in your page directive:

<%@ Page MasterPageFile="~/MyMaster.master" ...

Odds are, you probably don't need to actually get to the control. Rather, you need to set something in the master page. I'd just use an internal method to do what you need to do:

public partial class MyMaster: MasterPage
{
    internal void SetTheFoo(string foo)
    {
        this.WhateverControl.Text = foo;
    }

    //etc...
}

Then, from your page, just call it:

Master.SetTheFoo("Foo");

If you still need to get to the control, then in your master page, you could add a public property exposing your modal popup extender.

public AjaxControlToolkit.ModalPopupExtender MyModalPopup
{
    get { return this.TheNonPublicModalPopupExtenderControl; }

}

这篇关于查找控制在母版返回nulll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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