views:

5348

answers:

4

I am trying to change a label's text by using server-side javascript (onclick) and C# within the page_load event. For example, I would like to write something like the following:

Label1.Attributes.Add("onclick", "Label2.text='new caption'")

Does anyone know the correct code for this? Also, what is this type of code referred to; is it just javascript or javascript in C# or is there a specific name? Lastly, does a book or online resource exist that lists the choices of control.attributes.add("event", "syntax") code to use with C#?

Thank you,

DFM

+2  A: 

There is no server-side Javascript (unless you change to a platform other than ASP.NET where you actually use Javscript as server language). What you are doing is adding an attribute to the html tag, and the code will be executed entirely on the client side.

First, let's look at how it's done in HTML without the server side code and server side controls:

<span onclick="document.getElementById('Label2').innerHTML='Thank you';">Click me</span>
<span id="Label2"></span>

To use Label controls instead, setting the onclick attribute from server side code, you would do like this:

Label1.Attributes.Add("onclick", "document.getElementById('Label2').innerHTML='Thank you';");

This will work as long as the controls are not inside a naming container. If they are, the id of the controls are prepended with the name of the container to keep them unique, so you need to use the ClientID property to find out what their final id is:

Label1.Attributes.Add("onclick", "document.getElementById('" + Label2.ClientID + "').innerHTML='Thank you';");

The ClientID always contains the id that you can use to access the element from Javascript, so the last code always works regardless if the control is in a naming container or not.

To find out what attributes you can use, you should look at the HTML documentation, for example the Internet Explorer documentation for the span element. When looking at the documetation for a specific feature, notice the Standards Information, as that will tell you if it works in any browser or just in Internet Explorer.

Guffa
There is server-side Javascript (see http://en.wikipedia.org/wiki/Server-side_JavaScript) but it doesn't handle on-click events!
Matthew Flaschen
@Mathew: Thanks for the clarification. I will add a parenthesis about it. :)
Guffa
Thank you! Aside from C#, control.attributes.add... are formally called label controls? The reason why I ask is because I am trying to find a nice consolidated list or resource that I can familiarize myself with. I appreciate your helpful feedback.
@DFM - I put a link to an MSDN article on Using JavaScript Along with ASP.NET in my answer
Russ Cam
@DFM: No, it's not calles Label controls, I used Label controls in the example as that is what you seem to try to use in your question. They are called HTML attributes. Take a look at the documentation that I linked to.
Guffa
A: 

The code above adds JavaScript to a server control rendered on the client. Take a look at this MSDN article - Using JavaScript Along with ASP.NET for more information.

IIRC, you will need to reference Label2 by its ClientID and will need to write some JavaScript to change the label's text value (I think ASP.NET labels get rendered as <span> tags).

Russ Cam
A: 

After that I tried to detect the change of text but it returns nothing

if Label.text = "Thank You" then Label.text = "Hi"

I am using ModalPopUp on my linkbutton to open my Panel. And the Label will show 'Thank You.'

LinkButton.Attributes.Add("OnClick", "document.getElementById('" + Label.ClientID + "').innerHTML = 'Thank You'")

Any Idea how to detect the label Value in code behind page?

Ron Chong
A: 

lottory

Raman Lahun