Extending server-side controls with MS Ajax

Source: Internet
Author: User

Ms Ajax enables you to extend the characteristics of a server-side control after it is rendered to the client, making its interface more user-friendly.
Instance code: Iscriptcontrol.rar
First, create a Web site, select ASP.net ajax-enabled web site.
Add a class to the project, derive it from the textbox, and implement the IScriptControl interface. The following code example:

public class Sampletextbox:textbox, IScriptControl

Three, this control we will implement two properties:
The style after which the highlightCssClass control gets the focus. Enables the control to be highlighted when it has the focus.
nohighlightCssClass the style of the control that lost focus.

public string highlightCssClass
{
get {return _highlightcssclass;}
set {_highlightcssclass = value;}
}

public string nohighlightCssClass
{
get {return _nohighlightcssclass;}
set {_nohighlightcssclass = value;}
}

Iv. implementation of interface IScriptControl.
Getscriptdescriptors () returns an array of scriptdescriptor types that contain the properties and event handles of the control client instance.
GetScriptReferences () returns an array of scriptreference types containing the control client's JavaScript code.
In this example, we use four functions to implement these two functions. Code in:
Protected virtual ienumerable<scriptreference> getscriptreferences ()
{
ScriptReference reference = new ScriptReference ();
Reference. Path = Resolveclienturl ("SampleTextBox.js");

return new scriptreference[] {reference};
}

Protected virtual ienumerable<scriptdescriptor> getscriptdescriptors ()
{
ScriptControlDescriptor descriptor = new ScriptControlDescriptor ("Samples.SampleTextBox", this. ClientID);
Descriptor. AddProperty ("highlightCssClass", this. highlightCssClass);
Descriptor. AddProperty ("nohighlightCssClass", this. nohighlightCssClass);

return new scriptdescriptor[] {descriptor};
}

Ienumerable<scriptreference> iscriptcontrol.getscriptreferences ()
{
return getscriptreferences ();
}

Ienumerable<scriptdescriptor> iscriptcontrol.getscriptdescriptors ()
{
return Getscriptdescriptors ();
Five, this volume control. The code is relatively simple, so no more telling, into the following:
protected override void OnPreRender (EventArgs e)
{
if (!this. DesignMode)
{
Test for ScriptManager and register if it exists
SM = scriptmanager.getcurrent (Page);

if (SM = = null)
throw new HttpException ("A ScriptManager control must exist on the" ");

Sm. Registerscriptcontrol (this);
}

Base. OnPreRender (e);
}

protected override void Render (HtmlTextWriter writer)
{
if (!this. DesignMode)
Sm. RegisterScriptDescriptors (this);

Base. Render (writer);
}
Below is the complete code for our newly added class:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Collections.Generic;

Namespace Textboxextender
{
/**////<summary>
Summary description of SampleTextBox
</summary>
public class Sampletextbox:textbox, IScriptControl
{
private string _highlightcssclass;
private string _nohighlightcssclass;
Private ScriptManager SM;

public string highlightCssClass
{
get {return _highlightcssclass;}
set {_highlightcssclass = value;}
}

public string nohighlightCssClass
{
get {return _nohighlightcssclass;}
set {_nohighlightcssclass = value;}
}

protected override void OnPreRender (EventArgs e)
{
if (!this. DesignMode)
{
Test for ScriptManager and register if it exists
SM = scriptmanager.getcurrent (Page);

if (SM = = null)
throw new HttpException ("A ScriptManager control must exist on the" ");

Sm. Registerscriptcontrol (this);
}

Base. OnPreRender (e);
}

protected override void Render (HtmlTextWriter writer)
{
if (!this. DesignMode)
Sm. RegisterScriptDescriptors (this);

Base. Render (writer);
}

Protected virtual ienumerable<scriptreference> getscriptreferences ()
{
ScriptReference reference = new ScriptReference ();
Reference. Path = Resolveclienturl ("SampleTextBox.js");

return new scriptreference[] {reference};
}

Protected virtual ienumerable<scriptdescriptor> getscriptdescriptors ()
{
ScriptControlDescriptor descriptor = new ScriptControlDescriptor ("Samples.SampleTextBox", this. ClientID);
Descriptor. AddProperty ("highlightCssClass", this. highlightCssClass);
Descriptor. AddProperty ("nohighlightCssClass", this. nohighlightCssClass);

return new scriptdescriptor[] {descriptor};
}

Ienumerable<scriptreference> iscriptcontrol.getscriptreferences ()
{
return getscriptreferences ();
}

Ienumerable<scriptdescriptor> iscriptcontrol.getscriptdescriptors ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.