Results 1 to 2 of 2

Thread: Can client JavaScript work on Server controls?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    MA, US
    Posts
    78

    Can client JavaScript work on Server controls?

    Hi,

    Can I control from JavaScript (on the client) properties of the server control.
    I have some server controls (textbox) and I want to put an html control (checkbox) that will fill some values on the server control without making another trip to the server.
    If this is possible, how should I access the server controls into client code?

    Thank you,

    Sorin

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Sure, all a server control does is end up producing html for the client, so the client can interact with it as though it has no clue that anything is a server control, to see just view the source of a client side page that was built using server controls, it's just plain ol' html.

    Before rendering to the client:
    Code:
    <%@ Page Language="vb" 
    		 AutoEventWireup="false" 
    		 Codebehind="ClientServer.aspx.vb" 
    		 Inherits=".ClientServer"%>
    <html>
    	<head>
    		<title>ClientServer</title>    
    	</head>
    	<body>
    		
    	<form id="Form1" method="post" runat="server">
    		<input 
    			type="button" 
    			id="ClientSideControl" 
    			value="Click Me"
    			onclick="document.Form1.ServerSideControl.value = 'Hello World'"/>
    		<asp:TextBox 
    			ID="ServerSideControl" 
    			Runat="server"/>
    	</form>
    
    	</body>
    </html>
    After rendering to the client:
    Code:
    <html>
    	<head>
    		<title>ClientServer</title>    
    	</head>
    	<body>
    		
    	<form name="Form1" method="post" action="ClientServer.aspx" id="Form1">
    <input type="hidden" name="__VIEWSTATE" value="dDwyMTA1NTI4MTE3Ozs+uLuvFCnFMUU8IA+RBGwlVJTVfwM=" />
    
    		<input 
    			type="button" 
    			id="ClientSideControl" 
    			value="Click Me"
    			onclick="document.Form1.ServerSideControl.value = 'Hello World'"/>
    		<input name="ServerSideControl" type="text" id="ServerSideControl" />
    	</form>
    
    	</body>
    </html>
    One thing to remember, since there is no state between the client and server(as in javascript will only know what the client knows) you cannot access any special server control properties that would be accessible from the server.

    For example, the asp:TextBox has a Text property. All that happens is that whatever is set in the Text property of a server control is mapped up to the Value attribute of the input tag that is eventually generated and sent to the client. The clientside code can mess around with the Value attribute using javascript or whatever but it can't do anything with the Text property because only the server(and .Net framework) knows about that one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width