Mar 14

This article show you how to create a Asp.net server control that you use directly in the client-side via javascript. As it shows bleow:

<my:MyButton ID="btTest" runat="server" ClientInstanceName="testButton" Text="Hello world" />

testButton.SetBackgroundColor('#FF0000');

This article introduces 2 concepts:

  • Object oriented JavaScript
  • Create a custom asp.NET control which implements this JavaScript.

Object Oriented Javascript

Create class

First we have to know how to create and instantiate classes in JavaScript. Here is a very simple class declaration.

Constructors

You can define some parameters for your constructor; it's the same declaration as function parameters.

Public & private variables

You can also declare public and private variables; private viriables are declared "locally" with the key word 'var'. The public members are members of 'this' and will be declared as above.

We can see that only the 'publicVariable' is visible in the intellisense.

Functions

You can define functions the same way you define public members.

By this way, you can encapsulate an html control in a class, and create a friendly interface by adding some methods to modify the control.

The example show you how to allow changing the background color of a simple html control.

Creating a client instance for an Asp.NET server controls

Now we're going to see how to implements that to create client instance in a control server.

1. Create a new class library.

2. Add a JavaScript file which contains the 'ClientObject' class definition

3. To use the file as embedded resource, change the build action of the file.

4. Add this line in the AssemblyInfo.cs (File name : <completeNameSpace>.<fileNameWithExtension>)

5. Add a new class which extends System.Web.UI.Button

6. Add a property ClientInstanceName

7. If the ClientInstanceName property is defined, the script file must be included in the page.

This action must be done in the OnPreRender method.

8. Render the js instance, to achieve it, override the Render methods, and write a little js script.

Now, you can use your control in your aspx like this

<my:MyButton ID="btTest" runat="server" ClientInstanceName="testButton" Text="Hello world" />

You can now manage your Asp.NET in client side, in this example; you can change the background color of the button:

<input type="button" value="red" onclick="testButton.SetBackgroundColor('#FF0000');" />

Generated Html:

<input type="submit" name="btTest" value="Hello world" id="Submit1" />

<script type="text/javascript" language="javascript">

var testButton = new ClientObject(document.getElementById('btTest'));

</script>

Thanks to Nicolas Castellano for this article.

Sample code: ClientControlSample.zip (38.03 kb)

Comments

The Mostly

Posted on Sunday, 27 June 2010 17:56

Thanks for the information, i'll bookmark this page for my references!

Noel Lutrell

Posted on Saturday, 10 July 2010 01:11

Thanks for providing this resource. it's just what I was looking for.

Alex Bundley

Posted on Tuesday, 13 July 2010 07:22

This is useful! How did you learn about this when you were a newbie?

Today Gossip and News

Posted on Thursday, 12 August 2010 14:21

Before I meet your pilot website, I incredible didn’t know something about this subject that I will use in my school task. After I read page by page, I found the significant thing for my initial idea, great article in your webpage, I discover many missing file and data in my book guidance, but you gave me the several options. I am postgraduate college and want to finish my subject; it is hard task (for me). I spent over 2 weeks, just browsing and finding this nice article.

Aleshia Kaigle

Posted on Monday, 16 August 2010 15:28

lol, thx!

durham mls

Posted on Wednesday, 18 August 2010 11:06

Hello,

I just wanted to let you know that I have been watching for a while and I would like to sign up for the feed. I'll give it a try but I will need some assistance. This is a good find and I would hate to lose contact, and maybe never find it again.

Anyway, thanks again and I look forward to posting again in the future!

wpia

Posted on Thursday, 19 August 2010 21:35

This was just what I was on  looking for. I'll come back to this blog for sure!

Paul Scholes

Posted on Tuesday, 24 August 2010 12:56

It’s very important that schools do have <A href="http://www.stitchfactory.co.uk">school uniforms</A> to help prevent the amount of money some kidshave spent on them, <A href="http://www.stitchfactory.co.uk">schoolwear</A> wise, compared to some kids whose family are not in a position to buy expensive clothes. This help to also reduce bullying.

Danny Schwarzenbach

Posted on Sunday, 29 August 2010 13:51

May I get permission from you to post an excerp of this on my blogger blog?

realtouch toy

Posted on Monday, 30 August 2010 03:37

I enjoy your blog, Its great to find not every person is just posting a lot of crap now a days!

MexicoFishingDeals - Job

Posted on Friday, 3 September 2010 17:59

This is a nice website!, i just recently found it and now is on my Favorites.
Keep the effort of keeping this Blog updated, and if you have a chance please visit my site http://www.mexicofishingdeals.com
thanks and keep tight lines.
Job O.

hulu converter

Posted on Sunday, 5 September 2010 01:18

keep up to date with incoming post.

Hotel Granada Panama

Posted on Wednesday, 8 September 2010 01:16

what is with this people. ah, well i guess there isn't much you can do about it... i heard that askismet can block spammers .. maybe  you should look into it

panic away scam review

Posted on Wednesday, 8 September 2010 08:28

Hi there, Just wanted to mention your site is particularly good and I truly like the weblog design. Is it a made to order layout or some theme I can obtain as well?

Add comment




  Country flag

biuquote
Loading