Develop ASP.NET Custom Control (ASP.NET learning note three)

zhaozj2021-02-17  52

By Cash

2003.4.25

Previous chapter we wrote a simplest custom control, let us thrive, actually develop a practical custom control: a menu that can set permissions.

In this project, we will not use any unison theoretical knowledge, users only need to have basic DHTML knowledge. As you know, the server control is just a package of HTML and JavaScript. As for other techniques, we will gradually mention and apply, and finally produce controls with commercial level.

The idea of ​​making this control is simple: First use DHTML to write a menu, then extract it, then encapsulate the script and HTML display code, and finally rewrite the render method as the parameter. You look, it is so simple.

If you don't want to read the code through the browser (that is very annoying), you can download the source code of this project to the following address, including the source code for this custom control and various types of test files: Different data sources and permission settings.

http://user1.7host.com/cashtsao//cashmenu0110.zip

First declare an enumeration type in the namespace CashControls: MINMENUALIGN:

Public Enum MainmenualIgn

{

///

/// left

///

LEFT = 0,

///

/// middle

///

Center = 1,

///

/// right

///

Right = 2

}

Then declare the class CashMenu inherited from WebControls, declare the following variables and properties:

variable:

Private system.Web.httpContext contextObject;

Private system.xml.xmldocument oxmldoc = new xmldocument ();

PRIVATE STRING MENUDATA;

Private mainmenualign mainmenualign = mainmenualign.center;

Private string rulestring = "admin";

Private int cellspacing;

Private int mainmenuheight;

Attributes:

///

/// Attribute Menudata is used to specify the data source, which can be an XML file,

/// can also be a string of XML format

///

[Bindable (TRUE),

Category ("DATA"), Description ("Provides an XML data source for menu.")]]]

Public String Menudata

{

get

{

Return Menudata;

}

set

{

Menudata = Value;

Build ();

}

}

......

Disclaimer auxiliary method:

///

/// Method Build is used to bind the data source, bind it to the Context, call in the attribute menudata

///

Private void build ()

{

IF (Menudata! = NULL)

Try

{

CONTEXTOBJECT = this.Context;

IF (contextObject! = null)

OXMLDoc.Load (ContextObject.server.mappath (menuData));

Catch

{

Try

{

Oxmldoc.LoadXml (Menudata);

}

Catch (Exception E)

{

ContextObject.Response.write ("Opens the Data Source Failed!
");

ContextObject.Response.write (E.MESSAGE);

}

}

Return;

}

///

/// method generateHTML is used to generate an HTML code, call the TraverseMenutree method, pass a node in the Context in the CONTEXT.

///

///

Private string generateHTML ()

{

String temphtml = "";

IF (oxmldoc.childnodes.count! = 0)

{

Try

{

Temphtml = Temphtml TraverseMenutree (Oxmldoc.childNodes.Item (1) .firstchild);

}

Catch

{

Throw new argumentException ("An error occurred.");

}

}

Return (TemphTML);

}

转载请注明原文地址:https://www.9cbs.com/read-28479.html

New Post(0)