当控件不可见时,页面内容中母版页的Findcontrol不起作用 [英] Findcontrol of master page in page content is not working when control is invisible

查看:61
本文介绍了当控件不可见时,页面内容中母版页的Findcontrol不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个带有母版页和一些页面内容的.Net网站......我正在使用带有框架4.0和jquery 1.6的vb.net。我正在尝试显示主页面中存在的面板,默认情况下不可见。我正在尝试使用javascript从客户端的内容页面执行此操作。



以下是我在Master.aspx中的面板



So I have a .Net website with a master page and some page contents ... I am using vb.net with framework 4.0 and jquery 1.6. I am trying to show a panel that is present in master page and invisible by default. I am trying to do this from my content page in client side using javascript.

Below is my panel in Master.aspx

<asp:Panel ID="PanelMsg" runat="server" CssClass="SMSMSG" Visible="false">
<asp:Label ID="labMsg" runat="server">test test </asp:Label>
<input id="AnimationDiv" type="checkbox" onclick="addTotargetGroup(this);" />
<label class="AnimationDiv" for="AnimationDiv">
   An SMS has been sent to Insured.</label>
</asp:Panel>





my_page.aspx中的Javascript:





Javascript in my_page.aspx:

var PanelMsg = document.getElementById('<%=CType(Me.Master.FindControl("PanelMsg"), Panel).ClientID %>');
alert(PanelMsg);





在这种情况下,PanelMsg为null,但如果我将面板的可见性更改为true,则PanelMsg是[object HTMLDivElement]。所以主要的问题是我正在尝试获取panelMsg并在此处显示它,并更改标签值,但它在不可见时返回为null。那么我该如何解决这个问题呢?



我尝试过:



当面板可见且我成功时,我试图获得标签值:





In this case PanelMsg is null, BUT if I change visibility of panel to true, then PanelMsg is [object HTMLDivElement]. So the main problem is that I am trying to get the panelMsg and show it here, and change the label value, but it is returned as null when it is invisible. So how can I solve this problem?

What I have tried:

I have tried to get the label value when panel is visible and I succeed:

var labMsg = document.getElementById('<%=CType(Me.Master.FindControl("labMsg"), Label).ClientID %>');
alert(labMsg.innerHTML);







但是当我运行相同的代码时看不见的pan我得到一个错误:无法读取属性innerHTML为null。




But when I run the same code with invisible pan I got an error: cannot read property innerHTML of null.

推荐答案

当组件不可见时查看页面的来源,你看到你的PanMsg div在源头?不,因为如果它不可见,它不会被呈现或发送给客户端,那么你的js如何与它互动呢?



如果你想让一些东西可见客户端然后不要将Visible属性设置为false,而是改变它的样式并将样式设置为display:none;



抢占你的下一个问题google在asp:panel上设置样式以了解如何操作。
When the component is not visible view the source of the page, do you see your PanMsg div in the source? No, because if it's not visible it isn't rendered or sent to the client so how can your js interact with it?

If you want to make something visible on the client side then don't set the Visible property to false, instead alter it's style and set the style to "display:none;"

To preempt your next question google "set style on asp:panel" to find out how to do that.


2k89Tg1KHEPF
2k89Tg1KHEPF
You can do the following,

add property runat="server" to the control of master page which you want to access on the main page.(ingore if already added)

after that in the page load event of the page write following,

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Web.UI.MasterPage mp = this.Master;
        System.Web.UI.HtmlControls.HtmlInputText txtMasterPage = (System.Web.UI.HtmlControls.HtmlInputText)mp.FindControl("txtInputMaster");
        if(txtMasterPage.Visible == false)
        lblPage.Text = lblPage.Text + ":" + txtMasterPage.Value;
    }

this will definitely work


这篇关于当控件不可见时,页面内容中母版页的Findcontrol不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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