views:

124

answers:

5

Hi,

I try to show to user a message with javascript. I have a button and this button does something like,

Label1.Text = "Hello world";
//javascript code

Firstly, label1.text will "Hello world" after user will see the message.

thanks.

+1  A: 

If I understand you, you need an event handler on the button.

<button id="somebutton">Click me</button>
<label for="someid"></label>

The button can be any html, I just want to show the id, actually.

document.getElementById('somebutton').onclick = function() {
  document.getElementById('someid').innerHTML = 'Hello World';
  return false;
};

You can also just append a child to the label adding a textnode, if you wanted.

James Black
+1  A: 

Not sure exactly what you are asking here. I am not aware of a an HTML label. If you are speaking of an ASP:Label, that is rendered as a span in the browser. To set the value in JavaScript, you should access it using the document model. Below might help, if not, please try to clarify your question.

<HTML>

<head>
<script>
function changeText()
{
   document.getElementById('spnSayHello').innerHTML = 'Hello World';
}
</script>
</head>

<body>
<span id='spnSayHello' onclick="changeText();">clickme</span>
</body>
</HTML>

UPDATE: If I understand you correctly, you are trying to render javascript to the page via your c# code? You can do this in a number of ways. The way that I usually do this is using RegisterStartupScript like this:

private void writeSomeJs()
{
   ClientScript.RegisterStartupScript(this.form1.getType(),"jPopUp","<script>alert('Hello World');</script>")
}
J.Hendrix
I mean with "Hello world", I have c# code in code-behind. there are some code which I can't do it with javascript. So, after the c# codes, i have to show a message to user.
Can
+1  A: 

Probably the simplest way to execute JavaScript code after the Label's text has been set would be the following:

Label1.Text = "Hello world" +
    @"<script type=""text/javascript"">
          alert(""Message to show."");
      </script>";

Basically, the JavaScript code is placed inside the Label's Text. When the browser loads the page returned by the server, it will process the JavaScript and show the message.

Jon Benedicto
+2  A: 

C# and javascript code run in different timeframes and in different computers. Your server-side code runs and generates HTML which is then sent to browser. The browser then runs javascript code.

Therefore, you can't do stuff like setting the label text in C# and then run javascript. You can't 'call' javascript from C# or C# from javascript.

What you can do, however, is set the label text (which is rendered as a span in browser) in javascript and then do other stuff you wanted to do.

Chetan Sastry
that's about as close as i could make out as well
slashmais
A: 

Hi, I Suppose this is what you are trying, I tried out this code its working for me.

Label1.Text = "Hello world";

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "alert('Messages');", true);

or even you can call javascript function from here

Label1.Text = "Hello world"; 
     Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "function();", true);
GayaMani