admin管理员组文章数量:1300084
I am trying to access control in Master page from Content page(Asp) using javascript like this
alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').value);
control in Master page is as follow,
<asp:Label ID="lbl" runat="server" Text="one"></asp:Label>
But unfortunately it is not working. I am getting undefined value
I am trying to access control in Master page from Content page(Asp) using javascript like this
alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').value);
control in Master page is as follow,
<asp:Label ID="lbl" runat="server" Text="one"></asp:Label>
But unfortunately it is not working. I am getting undefined value
Share Improve this question edited Mar 12, 2014 at 17:34 user3411907 asked Mar 12, 2014 at 17:22 user3411907user3411907 211 gold badge1 silver badge4 bronze badges 5-
In what way is it not working? Do you get an
Exception
from .NET or anundefined
value in the alert? TheFindControl()
method is not recursive, so unless yourlbl
control is a direct children ofMaster
, it won't be found. – juan.facorro Commented Mar 12, 2014 at 17:26 - so what is the solution? how can I correct it? – user3411907 Commented Mar 12, 2014 at 17:29
- I get undefined value – user3411907 Commented Mar 12, 2014 at 17:33
-
Why you don't use a pure javascript solution? jQuery helps you a lot:
$("#parent").find(".contrl-class-name").val();
. If you give us more information, we'd give you better answer. – Maysam Commented Mar 12, 2014 at 17:34 - I know it works, I have worked on project where it's working fine. I am just missing something that's why I asking for help – user3411907 Commented Mar 12, 2014 at 17:38
5 Answers
Reset to default 3I noticed that you are actually accessing the .value
field of the element that the <asp:Label />
control generates, which is a <span></span>
. This type of element won't return anything for the .value
attribute. If you are actually trying to access its text then use:
alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').innerText);
or
alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').innerHTML);
The problem is that a master page is a naming container, hence the client id of the control receives a prefix which is the id of the naming container. Using JavaScript, it is easily solvable:
var elm = document.querySelector('[id$="lbl"]');
$= means, ends with.
Include jQuery in your page and use this script:
<script>
$(document).ready(function(){
alert($("#lbl").text());
});
</script>
This is work for me:(Check the below code)
alert(document.getElementById('<%=(Master.FindControl("lbl")).ClientID %>').innerText);
Using getElementById
didn't work for me. The following can be used instead:
$find('<%=((Label)Master.FindControl("lbl")).ClientID %>');
本文标签: javascriptAccessing controls in Master page from content pageStack Overflow
版权声明:本文标题:javascript - Accessing controls in Master page from content page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741654796a2390695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论