Click to See Complete Forum and Search --> : Client & Server side scripting


NikhilCK
May 18th, 2002, 08:22 AM
Can I have both client and server side scripting in my ASP.NET page at the same time? Can I use Javascript for client side scripting and C# for server side scripting? Also if I give runat=client to C# scriptlet block, what will happen? Can I do client side scripting like textboxes enabling and disabling with it? A code sample will be of great help.

robtaylor
May 20th, 2002, 06:44 AM
Q) Can I have both client and server side scripting in my ASP.NET page at the same time?

A) Yes you can! All you have to remember is that anycode which is server-side will (or should) force the browser to make a call back to the server.

Q) Can I use Javascript for client side scripting and C# for server side scripting?

A) Yes, that is no problem.

Q) Also if I give runat=client to C# scriptlet block, what will happen?

A) MMmmm, not sure - but as it is likely the browser does not understand C#, you will get an error. As far as I am aware, any code within the runat=client section is not reviewed by the .NET compiler, but instead it is sent straight to the browser. But I could be wrong and you should check this point.

Q) Can I do client side scripting like textboxes enabling and disabling with it? A code sample will be of great help.

A) Yes, it is very easy. If you want to disable a textbox using a button, set the onclick method of the button to point to an own function (clientside). In the function call the ID name of the textbox and set the DISABLED property to TRUE. This should disable it. To renable it, set the property to FALSE.

Hope this helps!

Rob


#########################################
Code Listing attached as a zip file

Code uses 2 buttons - 1 to enable the textbox, 1 to disable.
Remember not to have the buttons posting back to the server.
They must be clientside.

NikhilCK
May 20th, 2002, 10:09 AM
Thanks for the reply. The only thing I am not very sure right now is runat=server attribute of simple controls like textboxex, labels. Can anyone guide me in this.
Thanks,
Nikhil

robtaylor
May 20th, 2002, 11:13 AM
This is so that when elements such as text boxes are included as part of the webform, that you don't have client side scripting. Instead, the server will process everything. When you use a drop down list box, a call will be made back automatically to the server to process the event. The same if you enter text in a textbox. Once you have finished entering the text, it can be automatically processed at the server.

There are a number of advantages and disadvantages to this type of operation. The main being:

With server side processing you don't have to worry as much about compatibility of the browser (i.e. whether it is IE, Netscape or other) as the only data sent to the browser is in HTML format.

If elements are run on the server, all support code (i.e. for reacting to events etc) can therefore be placed together, giving you software more simplicity and structure.

Additionally there are a number of validation routines provided by the .NET framework, which can validate and check the data entered in text boxes and forms.

With the use of Code Behind files, it is possible to have a more programmatic (hence structured) approach to development of web applications. They now start to function more like real Windows applications. We can programmatically access objects (be it text boxes, other GUI elements or more business objects like database connections etc), and also handle events which occur.

However, this means many more round trips between client and server. I personally don't like this and therefore use a combination of both client side and server side programming.

Essentially it really doesn't matter whether you have you GUI elements run at server or run on the client. The main point is to have the code in the correct place to support these elements, i.e. the onclick event code should be placed in the HTML page if an element is run on the client. If run on the server it should appear in server code there (either inline or code-behind).

NikhilCK
May 20th, 2002, 10:13 PM
well thanks for your guidance man. Will come back to you in case of more queries;)