AJAX Extender Example: Button Enabled Changes
page 3 of 7
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 38265/ 132

Server Extender

The server extender's definition looks like the following skeleton.  Note the various uses of attributes.

Listing 1

[assembly: System.Web.UI.WebResource(
  "Nucleo.Web.ButtonControls.ButtonEnabledExtender.js", "text/javascript")
  ]namespace Nucleo.Web.ButtonControls
{
  [TargetControlType(typeof(IButtonControl)), ClientScriptResource(
    "Nucleo.Web.ButtonControls.ButtonEnabledExtender",
    "Nucleo.Web.ButtonControls.ButtonEnabledExtender.js")]
  public class ButtonEnabledExtender: ExtenderControlBase
  {
    ..
  }
}

In the AJAX Control Toolkit API there are two main base classes for developing custom controls: ExtenderControlBase and ScriptControlBase. The ExtenderControlBase class is used in this example because we are building an extender. For all extenders, these controls have a TargetControlID property; this property is linking the extender to the server control (and eventually underlying HTML element when it is rendered) that the extender control extends through JavaScript. The TargetControlType attribute is used to limit what type the controls can be.

Each server component has a server class (.cs or .vb) file and a JavaScript (.js) file. The WebResource and ClientScriptResource attributes map the server class to the javascript file and link the two together. The deployment model with custom AJAX controls is that JavaScript files are marked as embedded resources in Visual Studio to the client class that it represents. These files are later extracted using the framework components; no work is necessary for this on your part.

Moving along, to how easy the describing process is, the following properties and event handlers are illustrated below.  I will start by illustrating the properties.

Listing 2

[ExtenderControlProperty, ClientPropertyName("isEnabledInitially")]
public bool IsEnabledInitially
{
  get
  {
    return base.GetPropertyValue < bool > ("IsEnabledInitially"true);
  }
  set
  {
    base.SetPropertyValue < bool > ("IsEnabledInitially", value);
  }
}
 
[ExtenderControlProperty, ClientPropertyName("receiverControlID"),
  IDReferenceProperty(typeof(Control))]
public string ReceiverControlID
{
  get
  {
    return base.GetPropertyValue < string > ("ReceiverControlID"null);
  }
  set
  {
    base.SetPropertyValue < string > ("ReceiverControlID", value);
  }
}

The ExtenderControlProperty attribute maps the server control properties to a property in the client component; otherwise, a client-side error will be thrown if there is an invalid mapping. The ClientPropertyName attribute specified the name of the property on the client side if different than the property on the server-side. At runtime, the server will push down the initial values it defines to the client component.

One other important attribute to note is if you have any properties that reference another control (for both extenders and controls), the IDReferenceProperty allows for proper retrieval of the ClientID at description time.

For this extender, only one event is needed.

Listing 3

[ExtenderControlEvent, ClientPropertyName("enabledStatusChanged")]
public string OnClientEnabledStatusChanged
{
  get
  {
    return base.GetPropertyValue < string > ("OnClientEnabledStatusChanged",
      null);
  }
  set
  {
    base.SetPropertyValue < string > ("OnClientEnabledStatusChanged", value);
  }
}

This event allows an ASPX page to provide the name of a JavaScript method as an event handler, which gets called whenever an event is raised on the client side (more on this soon). At runtime, the name of this method has to be a valid JavaScript routine name, which will get called when the event is fired from the client component.

One other note: The GetPropertyValue and SetPropertyValue generic methods read/write data to/from ViewState; they are simply helper methods for this process. That is all from the server component. That is all the code that is needed at the moment to setup our ButtonEnabledExtender control.  It is that simple; the only item remaining is the client component.


View Entire Article

User Comments

Title: svfd   
Name: fgdfg
Date: 2012-12-12 7:36:25 AM
Comment:
egte
Title: hello   
Name: hello
Date: 2012-08-22 12:38:27 AM
Comment:
hello
Title: eeee   
Name: aaa
Date: 2012-08-08 2:21:21 AM
Comment:
jjjj
Title: dropdown list   
Name: Vrutti Shah
Date: 2011-11-23 4:47:12 AM
Comment:
I have two dropdown list. I get value of dropdownlist 1 from database. based on the selected value of dropdown 1, dropdown list 2 is populated. I have success in that but problem is that after select value from dropdown 1 page is load and data is lost from another tools like textbox(password type). pls do helpful.
my mail id is:vruttishah1986@gmail.com
Title: Useful information   
Name: pickatutorial.com
Date: 2010-10-05 11:30:12 AM
Comment:
Very useful article. Thanks.
Title: Reply: how to set data in text box based on ajax autocomple   
Name: Brian Mains
Date: 2010-03-11 3:22:38 PM
Comment:
Hello,

Auto complete has a client-side API; you would have to look to see an event that fires when the first text entry is selected, then set the value for textbox2.

HTH.
Title: how to set data in text box based on ajax autocomple   
Name: ieda
Date: 2010-03-10 4:04:46 AM
Comment:
i have two text box.Texbox1 and textbox2.Both use same table in database.Texbox1 use ajax extender autocomplete to select the data. What can i do if i want textbox2 set data aumatically from database after i select data for textbox1?
Title: Re:Button Extender   
Name: Brian
Date: 2008-10-06 3:08:21 AM
Comment:
I tested the code but i cannot achieve your test2 result.
Title: thanks   
Name: Karthikeyan
Date: 2008-09-24 7:54:24 AM
Comment:
Nice article.
Title: thanks   
Name: rezagolab
Date: 2008-09-24 3:10:50 AM
Comment:
hi
thanks
Title: Re:Button Extender   
Name: NamNguyenLe
Date: 2008-08-28 10:15:30 PM
Comment:
Thanks for cool article.
But it will be better if code is avaiable.
Please post it please.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-08-18 5:20:39 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search