server controls and Page Methods

J

jasonmwilkerson

Can you access properties (Text, Visible, etc...) on server controls
(i.e. TextBox, Label, etc...) from javascript? I am trying to toggle
the visibility of a Label clientside.

<script type="text/javascript">
function toggle() {
var label = $get('<%=this.lblMessage.ClientID %>');
if (label != null) {
label.Visible = !label.Visible;
}
}
</script>
<asp:Label ID="lblMessage" runat="server" Text="Hello" />
<asp:Button ID="btnToggle" runat="server"
OnClientClick="toggle();return false;" />

Is this possible?
Thanks!
Jason
 
G

grava

Can you access properties (Text, Visible, etc...) on server controls
(i.e. TextBox, Label, etc...) from javascript? I am trying to toggle
the visibility of a Label clientside.

<script type="text/javascript">
function toggle() {
var label = $get('<%=this.lblMessage.ClientID %>');
if (label != null) {
label.Visible = !label.Visible;
}
}
</script>
<asp:Label ID="lblMessage" runat="server" Text="Hello" />
<asp:Button ID="btnToggle" runat="server"
OnClientClick="toggle();return false;" />

Is this possible?
Thanks!
Jason

You've got Dom Properties for that:

var label = $get('<%=this.lblMessage.ClientID %>');
if (label.style) {
label.style.display = "hidden";
}
}

Or you can prototype all the properties of Label (for example) and wrap all
methods ... I think it's a suicide !

HTH
 
S

Scott Roberts

Can you access properties (Text, Visible, etc...) on server controls
(i.e. TextBox, Label, etc...) from javascript? I am trying to toggle
the visibility of a Label clientside.

<script type="text/javascript">
function toggle() {
var label = $get('<%=this.lblMessage.ClientID %>');
if (label != null) {
label.Visible = !label.Visible;
}
}
</script>
<asp:Label ID="lblMessage" runat="server" Text="Hello" />
<asp:Button ID="btnToggle" runat="server"
OnClientClick="toggle();return false;" />

Is this possible?
Thanks!
Jason

The server controls themselves cannot be manipulated via javascript, but the
HTML generated by the server controls can. I believe that a Label renders as
a <span> element, which may or may not contain the functionality you desire.
Personally, I would wrap the label in a <div> element and then toggle the
visibility of the <div>.
 
G

grava

Scott Roberts said:
The server controls themselves cannot be manipulated via javascript, but
the HTML generated by the server controls can. I believe that a Label
renders as a <span> element, which may or may not contain the
functionality you desire. Personally, I would wrap the label in a <div>
element and then toggle the visibility of the <div>.

Ops ... yes .. you've got to wrap into a div to set the "display" style !

Sorry.
 

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

Top