views:

591

answers:

4

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 am using asp.net)

My code in javascript (but not working):

 lths = document.getElementById("<%= lblhs.ClientID %>");
 lths.innerHTML = 'this is text line"
+3  A: 

You can find it using document.getElementById('<%= myControl.ClientID %>') or if you are using .NET 4.0 you can directly set the id field to be pulled by javascript. Alternatively you can use jquery's selectors. Your code isn't working because the Literal control doesn't render as an element, just static text. You can place a span around it, or use a Label control, which renders as a span for you.

Yuriy Faktorovich
document.getElementById('<%= myControl.ClientId %>') (not working)
Rajesh Rolen- DotNet Developer
i am using framework 2.0 (vs2005)
Rajesh Rolen- DotNet Developer
Changing to a Label should do the trick.
Yuriy Faktorovich
should be myControl.ClientID
Lachlan Roche
@Lachlan fixed.
Yuriy Faktorovich
their is no issue with lable.. issues is with only literal control
Rajesh Rolen- DotNet Developer
A: 

The <%= myControl.ClientId %> technique does not work when you are using master pages. That could be the case. What you can do is: Take a hidden field. Save the ID of the label in the hidden field. Access the value attribute to get the ID and then use document.getElementByID('HiddenField.Value') to get the control.

Aseem Gautam
+1  A: 

I don't think that you can access a literal control from your javascript client code because a literal control just renders it's pure value without any surrounding tags. So you don't really have an element which you could get by id.

What you could to is changing the literal control to a label control. This renders out as a html span tag by default. Like that you can select it on the client side.

Another option would be to write a span or div tag in your literal control on the server side like this:

myLiteral.Text = "<div id=\"myDiv\">Some Text here</div>"

on the client you could select the elemnt by using:

var myDiv = document.getElementById('myDiv');
Andre Kraemer
A: 

Just to rule out the obvious (since no one else mentioned it), this is a syntax error in JS:

lths.innerHTML = 'this is text line"

Not sure if this was a typing error here, or if you copied it from your code. You can use either " or ' but not both to surround a string. Also, you should use terminating semi-colons as best practice (although it's not required).

lths.innerHTML = 'this is text line';
bmoeskau