Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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>


Javascript in my_page.aspx:

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


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



But when I run the same code with invisible pan I got an error: cannot read property innerHTML of null.
Posted
Updated 28-Mar-17 3:20am

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.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900