namespace Panahy.Ajax
{
public class PassTextBox : TextBox, IScriptControl
{
private ScriptManager sMgr;
public string WeakCssClass;
public string MediumCssClass;
public string StrongCssClass;
protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor descriptor =
new ScriptControlDescriptor(“Panahy.Ajax.PassTextBox”, this.ClientID);
descriptor.AddProperty(
“weakCssClass”, this.WeakCssClass);descriptor.AddProperty(“mediumCssClass”, this.MediumCssClass);
descriptor.AddProperty(“strongCssClass”, this.StrongCssClass);
return new ScriptDescriptor[] { descriptor };
}
protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference reference = new ScriptReference();
reference.Assembly =
“Panahy.Ajax”;reference.Name = “Panahy.Ajax.PassTextBox.js”;
return new ScriptReference[] { reference };
}
protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
//test for the existence of a ScriptManager
sMgr = ScriptManager.GetCurrent(Page);
if (sMgr == null)
throw new HttpException(“A ScriptManager control must exist on the page.”);
sMgr.RegisterScriptControl(this);
}
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
if (!this.DesignMode)
sMgr.RegisterScriptDescriptors(this);
base.Render(writer);
}
IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
{
return GetScriptReferences();
}
IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
{
return GetScriptDescriptors();
}
}
}
Now we can reuse the control in any project that refers to the dll.
<%@ Register Assembly=”Panahy.Ajax” Namespace=”Panahy.Ajax” TagPrefix=”panahyAjax” %>
<panahyAjax:PassTextBox ID=”textbox1″ runat=”server” width=”200″
TextMode=”Password” WeakCssClass=”weak” MediumCssClass=”medium”
StrongCssClass=”strong”></panahyAjax:PassTextBox>
Although this sample can be handy to use as a template for other server controls, the introduced functionality could be done by creating an AJAX Behavior for client controls, which is the topic for the next article.