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
Add a ment  | 

7 Answers 7

Reset to default 3

ASP.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