why does this code return an empty Alert window?

P

Phil

Hi,

This code produces an empty Alert window. No error.
How to get the value into the Alert window?

Thanks
Phil

aspx file (content page):
------------------------
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
asp:Label ID="Label1" runat="server"></asp:Label>

<asp:Button ID="Button1" runat="server" OnClientClick="test()" />
<script language="javascript" type="text/javascript">
function test()
{
var x = document.getElementById('<%=label1.ClientID%>').innerText;
alert(x)
}
</script>
</asp:Content>


code-behind:
 
D

David Wier

The label has no text. I tried it - copied and pasted, exactly, except for
adding a "<" bracket in froont of the label, and adding a text property to
it. It worked just fine for me, then.
 
J

Jan

Hi David. Thanks for repying, but i really don't understand what you mean.
Can you tell me what i have to change in my code?
Thanks
 
P

Phil

Ok, thanks, but could you tell me why it doesn't work programmatically like
this:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click Label1.Text = "Hello World"
End sub
 
P

Phil

It's because the ClientClick will be executed first, and at that moment, the
value is not yet put into 'text' of the label
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top