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.
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.
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.
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>")
}
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.
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.
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);