why does this code return an empty Alert window?

  • Thread starter Thread starter Phil
  • Start date Start date
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:
 
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.
 
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
 
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
 
It's because the ClientClick will be executed first, and at that moment, the
value is not yet put into 'text' of the label
 
Back
Top