Javascript and controls with runat=server?



Hello everyone,

I am have couple of bad concepts and would like to clear things up. As
I understand ASP.NET server controls contain runat=server tag and they
only run on the server. But I've just seen an example where javascript
can access server side controls. Now javascript is about all things
CLIENT, so how can I access server controls from my javascript, here is
one such example
<%@ Page Language="C#" AutoEventWireup="tue" CodeFile="Default.aspx.cs"
Inherits="__Default" %>
<script language="javascript" type="text/javascript">

function access()
<form id="form1" action="default.aspx" runat="server" method="post">
<asp:HiddenField Value="He" runtat="server" ID="hidden" />

Now I am confused how can I access "form and hiddenfield" in my
javascript function. I am accessing server side controls in my
javacript code? How is that possible and more importantly HOW IS THAT
HAPPENING ( What makes it possible to access server side variables from
client side code)?
Please pardon my ignorance. I know this sounds like a basic question
but I need to clear up my concepts. Any help will highly be

bruce barker \(\)

most serverside control render some html. if you know what this html looks
like, you can access from javascript, which can access any html on the page.
the server control will usually renders some html element with the id of
ClientId. so try:

function access()

-- bruce (



Thank you for your reply. I understand your explanation partially, I
mean from what you wrote I came to know about another way of aceessing
server side controls from java script ;)

I will appreciate if someone else can explain my question, I will
appreciate that.



Hi, since the server controls are rendered to the browsers as ordinary HTML
elements, of course you can access the server control by locating the ID of
the element in the JavaScript. However, there are no access to properties of
ASP.NET server controls in JS. Any changes made upon the controls using JS
may not be saved to its ViewState too.

Alvin Chooi



Brice is right and you need to understand one general principle - whatever
happens on the server side the end result is html code that is sent to the
client browser. Simple example:
place textbox control on the page, run the project, view source of the page
( internet explorer go to view->source) what do you see, do you understand
TextBox web server control rendered html similar to this:
<input name="textBox1" type="text" id="textBox1"/>
Any scripting language (such as javascript) is strictly client side thing -
it's part of the DOM (document object model) and allows you to interact with
generated html document (i.e. change the color of the text, navigate to
different page, display message box, get some data from the server via AJAX,
open popup window, etc.) Please read an introduction to (for
instance: to understand the
basic idea behind and everything becomes clear.

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