问题
I have got a literal control on page (with some data on it). i want to access it in javascript and want to put some text on it. please tell me how can i access literal control in JavaScript. I was trying with following code-
<asp:Literal ID="lblPerInstanceListing" runat="server"></asp:Literal>
Javascript:
var value= document.getElementById('<%=lblPerInstanceListing.ClientID %>')
I am getting null value return by this.
回答1:
An ASP.NET Literal control does not by itself insert any HTML into a webpage. Your Literal control is a placeholder for text or HTML you will set in your code behind.
You should wrap your literal in a DIV or SPAN and give that an ID and reference that DIV or SPAN in the JavaScript
WebForm:
<span id="yourId"><asp:Literal ID="lblPerInstanceListing" runat="server"></asp:Literal></span>
JavaScript: Solved by this code.
var value= document.getElementById('yourId').innerText;
回答2:
<span id="yourId" runat="server"></span>
js :
var value= document.getElementById('yourId');
It is not better then use this code?? You can put in span enableviewstate to false if You need.
回答3:
It's a Literal
. It renders literal HTML to the page. It doesn't render any container matching the ClientID
that you can access on the client side.
Either put a div
or span
in the literal with an ID you can use, or use e.g. a Label
or Panel
control that does render a wrapping span
or div
you can use.
来源:https://stackoverflow.com/questions/15023975/want-to-access-literal-value-into-javascript