Asp.net| Notes | control
In the previous chapter we wrote a simple custom control that lets us put aside the theory and actually develop a useful custom control: a menu that can set permissions.
In this project we will not adopt any theoretical knowledge that is not learned, users need only have basic DHTML knowledge. As you know, server controls are just encapsulation of some HTML and JavaScript. As for other techniques, we will gradually mention and apply them in the future, eventually producing a commercial-level control.
The idea of making this control is simple: First use DHTML to write a menu, then extract the attributes, then encapsulate the script and HTML display code, and finally rewrite the Render method as a parameter. You see, it's that simple.
If you do not want to read the code through the browser (which is really annoying), you can download the source of the project at the following address, including the source code of the custom control and various types of test files: Different data sources and permissions settings.
Http://user1.7host.com/cashtsao//CashMenu0110.zip
First declare an enumeration type Mainmenualign in the namespace Cashcontrols:
public enum Mainmenualign
{
<summary>
Left
</summary>
Left=0,
<summary>
Middle
</summary>
Center=1,
<summary>
Right
</summary>
right=2
}
Then declare the class Cashmenu inherited from WebControls, declaring the following variables and attributes:
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;
Property:
<summary>
Property Menudata is used to specify a data source, which can be an XML file,
can also be a string of XML format
</summary>
[Bindable (True),
Category ("Data"), Description ("provide the XML data source for menu.")]
public string Menudata
{
Get
{
return menudata;
}
Set
{
Menudata = value;
Build ();
}
}
......
To declare a helper method:
<summary>
The method build is used to bind the data source, binding it to the context, and calling in the attribute Menudata
</summary>
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 ("Open Data source failed!<br>");
ContextObject.Response.Write (E.message);
}
}
Return
}
<summary>
Method generatehtml is used to generate HTML code, call the Traversemenutree method, and pass a node in oXMLDoc in the context.
</summary>
<returns></returns>
private String generatehtml ()
{
String temphtml = "";
if (OXmlDoc.ChildNodes.Count!=0)
{
Try
{
temphtml = temphtml + Traversemenutree (OXmlDoc.ChildNodes.Item (1). FirstChild);
}
Catch
{
throw new ArgumentException ("error occurred.");
}
}
return (temphtml);
}