admin管理员组文章数量:1290984
I am trying to set a text to an asp label from javascript, this is what i tried but it doesnt work
document.getElementById("Label1").value = "new text value";
<asp:Label ID="Label1" name="Label1" Font-Size="XX-Large" runat="server" Text="I am just testing"></asp:Label>
I am trying to set a text to an asp label from javascript, this is what i tried but it doesnt work
document.getElementById("Label1").value = "new text value";
<asp:Label ID="Label1" name="Label1" Font-Size="XX-Large" runat="server" Text="I am just testing"></asp:Label>
Share
Improve this question
asked Sep 6, 2012 at 11:27
VervatovskisVervatovskis
2,3675 gold badges29 silver badges46 bronze badges
2
- 1 If you're using 4 then add ClientIdMode="Static" to your label control - if not add <%=Label1.ClientID%> within your getElementById statement. See this resource for deeper explanation. weblogs.asp/asptest/archive/2009/01/06/… – Luke Baughan Commented Sep 6, 2012 at 11:30
-
.textContent
is the standard property for getting/changing the text but some older browsers don't support that which is why other people have been suggesting.innerText
or.innerHTML
. – Neil Commented Sep 6, 2012 at 12:14
7 Answers
Reset to default 3ASP.NET changes "Label1" to something like MasterPageContent_Label1 when rendered to the client. Also ASP.NET Label controls are renderd to the client as <span>
elements so you need to use innerHTML as opposed to value to set the content.
document.getElementById('<%= Label1.ClientID %>').innerHTML = "new text value";
Label1
is the server side ID of the Label control. Use the ClientID
to access it from the javascript. Try this:
document.getElementById("<%=Label1.ClientID%>").innerHTML= "new text value";
Hope this will help.
You can try this:-
document.getElementById("<%=Label1.ClientID%>").value = "new text value";
or you can try
var elMyElement = document.getElementByID('<%= Label1.ClientID %>');
elMyElement.innerHTML = "your text here";
You need to get the ClientID of the control in order to manipulate it in JavaScript.
The ClientID
is the Id
that gets rendered in the browser.
document.getElementById("<%=Label1.ClientID%>").value = "new text value";
Try this document.getElementById('<%= Label1.ClientID %>').InnerHTML = "Your Text Changed";
Use..
document.getElementById('<%=Label1.ClientID%>').innerText="New Text Value" ;
The asp label is rendered as a span so you need to set its innerHTML property not the value property, another option is to use JQuery and use the .text() method
本文标签: aspnetEditing an asp label from javascriptStack Overflow
版权声明:本文标题:asp.net - Editing an asp label from javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506500a2382353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论