Home  »  Code Snippets  »  ASP.Net  »

How to access server control from javascript with ClientID?

 Posted By : Manoranjan SahooPosted Date : 09/11/2011 07:49:47 AM Category : ASP.Net Points : 3

Sometimes we need to access our server control via javascript to make some process on client side. It is very easy when we use server control in the aspx page directly. if we use server control inside a server control like contentplace holder it will be difficult to access. Because it change the id as per the parent control. So, in this case we can use ClientID property to access the exact client id.

for Ex:

we have the code like below :


<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:Label ID="TimeDisplay" runat="server"></asp:Label>
</asp:ContentPlaceHolder>

In the above code we have one ContentPlaceHolder and one label inside the ContentPlaceHolder. We are going to display the time in this label. For this we are going to call javascript datatime method. So, we need to access the label from the javascript function to update. See the below code :

<script language="javascript">
function updateTime()
{
var now = new Date();
var labelobj=document.getElementById('<%=TimeDisplay.ClientID %>');
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
}
</script>

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.