Frog Frog Recommended: Frog Frog Learning ASP.NET Summary (one)

zhaozj2021-02-16  79

Knowing Microsoft's ASP.NET for a while, I feel that ASP.NET has a lot of improvements in functional and performance (before, I have been writing ASP programs), and summarizes some content, here and Everyone share it. Because the author has little experience, there is a technical mistake in the article or to improve the way, welcome to communicate with me.

Since individuals prefer to use C # languages, most of them use C # examples, but here is mainly what prototypes, so there is no excessive dependence on specific language, if you are more biased to VB can refer to This article is temporarily converting his own thoughts:

Code change skin - C # and VB

Http://blog.9cbs.net/onlytiancai/archive/2004/07/16/43137.aspx

VB.NET and C # syntax comparison

http://blog.9cbs.net/onlytiancai/archive/2004/07/16/43134.aspx

I want to talk about my experience from the following aspects, mainly want to write some choices from safety and performance consideration.

1.Web user control, web custom control, background encoding class, .NET component usage:

2. Cache (Cache and Application) Use:

3. Use occasions for Web services and components:

4. VB.NET and C # usage:

5. DataReader and DataSet usage:

6. Use occasions for database and XML data:

7. Use occasions of string connectors and StringBuilder.

8. Windows and Form authentication based use occasions.

9. Use of static methods and non-static methods.

10. HTML controls and server controls.

11. Client technical technology and service technology use occasions.

12.DTD and Schema's use occasions

13. Use occasions of string handle functions and regular expressions.

14. Synchronization of Web Services Use and use occasions for elimination calls

15. Use occasions of instantiation and inheritance class

16. Use occasions of single-value binding and multi-value binding

1. Web User Control, Web Custom Control (Custom Server Control), the use of the background encoding class, .NET component (assembly):

These technologies are used to reuse the code, but the useful occasions are different, and I will try my best to explain the differences between their respective and between them.

Let's take a look at the feature of web user controls and web custom controls:

Web user control

Web custom control

Web user controls are easy to create, web custom controls are difficult to create, only provide limited support for users who use visual design tools to provide a full visual design tool for users to support a separate copy of the control in each application only A single copy of the control is required in the global program cache cannot be added to the toolbox in Visual Studio to add the toolbox in Visual Studio. Suitable for static layout, suitable for dynamic layout.

In summary, web user controls apply to:

1. Elements on the page, such as headings, product catalogs, menus, registration controls, etc.

2. Utilize the cache function of the user control, the cache is often browsing, and can increase the performance of the page.

3. Pack the duplicate page element into the user control, reduce the amount of code on each page.

However, if you want to create a more widely used control than your application, you need to consider using a web custom control.

If you have MSDN installed, it is possible to refer to the link below with the recommended use of web user controls and web custom controls.

MS-help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/vbcon/html/vbconWebuserControlsvscustomwebControls.htm

For example to explain how to build user controls:

<% @ Control Language = "C #"%>

Hello, I am a frog frog to save the above code as WAWAUC.ASCX. This user control is built. (You need to specify this address when calling user controls)

Let's take an example to explain how to cache user controls. After writing a good control, you can implement the cache (fragment cache) of the control in the top of the .ascx file.

<% @ Outputcache duration = "60" location = "any" varybycustom = "browser" VarybyParam = "none"%>

Use users frequently accessible but not frequently updated data into user controls and caching up, and some real-time information do not cache, so that the page overall performance can be improved, and some real-time information cannot be updated in time.

Simply explain several parameters of OutputCache, this tag can be used in .aspx and .ascx.

OutputCache: Use a cache.

DURATION: Specifies the time of the cache content, the unit time

Location: When set to Server, only the network agent is only available to DowNStream when setting to DOWNSTREAM, and set to the client to indicate that the browser can cache the page, set to any, indicating that these caches can be used.

VarybyControl: Allows controls on the cache page, which is used on the .aspx page when this flag is.

VaryByParam: Fix different versions of cache based on the POST pass parameters, want to set the value to none at any time, if you want all parameters to use different versions of the cache to set the parameters *.

VarybyCustom: Forms different versions of cache according to different versions of browsers, or uses the specified string to form a different version of the cache, set to Browser, you can use different versions of the browser to use different versions, this in different browser It is useful to display data on the device.

VarybyHeader: Form a different cache according to different HTTP headers.

Custom Server Controls can reuse the code between different applications, you can start with HTML from the beginning to create custom controls, or create custom controls in existing server controls (generally call such controls as synthetic controls) . For example, how to set up a custom server control:

Using system;

Using system.Web.ui;

Namespace WAWACONTROLS

{

Public Class HelloControl: Control

{

Protected Override Void Render (HTMLTextWriter Writer)

{

Writer.write (""

Hello, Frog Frog

");

}

}

}

As can be seen, the custom user control inherits from System.Web.ui. Control, and rewrites the render method of the Control class to change the default output of the Control class, and then use the HTMLTextWriter control to output any HTML code and Script code, such a complicated custom control.

Save the above code as MyCustomControl.cs

After writing the code, you need to compile into a .dll file, complete the CSC command tool combo itself with .NET framework, first write a batch of processed files, as follows:

SET Assembly = System.dll, System.Web.dll

CSC / T: library / out: mycustomControl.dll MycustomControl.cs / r:% assemblyMblies%

PAUSE

Briefly introduce the use parameters of the CSC command:

/ t represents the output type, here use library, indicates that the output is the .dll file, and can also be an Exe (console program), WINEXE (Windwos program), etc., / out indicates the file name you want to output, followed by It is the file name you want to compile, / r indicates that you need to compile those files into the file you want to generate, here is the use of the SET command first define a variable, SYSTEM.WEB.DLL, Finally, use a PAUSE command to pause the output. Note that SET and PAUSE are the system's console (CMD) command, which is derived from the ancient DOS. Save the above code as a .bat batch file, the two-machine run can generate the .dll file, and finally put this file into the bin directory bouting. Some differences when using user controls and custom controls.

User Control Call Examples are as follows: First enter the following code in the beginning of the call control.

<% @ Register tagprefix = "wawauc" tagname = "testcon" src = "wawauc.ascx"%>

Then use the following code similar to:

Note The above code is to be placed in the server control FORM container.

Briefly describe the description of the various parameters of the Register.

TagPRefix: The collection name of the control group is the call control is part of the colon.

TAGNAME: The name of the control is the call control is part of the colon.

SRC: Source file address of the control

Namespace: Name control for control (for server controls)

AskEMBLY: Control Collection (User Server Control)

Custom Control Call examples are as follows: First, enter the following code in the beginning of the call control.

<% @ Register tagprefix = "customtrol" namespace = "wawacontrols" assembly = "MycustomControl"%>

Then use the following code similar to:

For details on creating user controls, you can refer to the address below:

MS-help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/vbcon/html/vbwlkwalkthroughcreatingwebuserControls.htm

For details on creating custom controls, you can refer to the address below:

MS-help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/vbcon/html/vbwlkwalkthroughcreatingcustomwebControls.htm

We all know that in the ASP program design, the user interface layer HTML code and the code of the business logic layer are inserted together. In order to change this, ASP.NET introduces a background encoding technology (codebehind), which can be used The displayed HTML is separated from the service logic code block. This can separately store the code of the business logic in a .vb or .cs format file, and then use the binding group in the front desk .ASPX.

Simply talk about the use of the background encoding class:

If you save the background's code as a WAWA.cs file, define a class_wawa class in the file, the front desk display file is Wawa.aspx

You can use the following statement at the beginning of the Wawa.aspx file to use the contents of the WAWA.cs file in the background.

<% @? Page language = "c #"? Src = "wawa.cs"? Autoeventwireup = "true"? Inherits = "class_wawa"%> Simply explain several functions that Page can use

Page: Set the page element.

Language: Setting the scripting language used by the page, can be C #, VB, etc.

SRC: Specifies the path and file name of the background encoded file, replaced with the CodeBehind parameter in the vs.net.

Autoeventwireup: Set whether to automatically activate the page's event, if you are using VS.NET to False, because VS.NET management page event, if you write, you can set it to true.

Inherits: Setting this page inheritance, that is, you can use this specified class.

After the application of the user control, custom control, and background coding class, let's take a look at the use of the assembly, the assembly is also a common code multiplexing technology, and some people are called .NET components, it can use VB.NET or C # to write, write it later, compile the.dll file, put it in the bin directory of the application directory so that you can reuse the business logic of this .NET component when writing the program, because it has been compiled .dll file, So, whether it is VB.NET written or written in C #, or written in other languages, this is no longer important, such a development team will be more tacit, because they don't have to extend for the language of love. The progress of development can use your business logic write components to use others.

If the assembly is not in a bin directory, you can load the program collective in other directories by modifying the web.config file, or you can load the assembly in the GAC. If you have not found the assembly you want to call, you will An error occurred.

Namespace WAWAComponents {

Public class wawa_hellocs {

Public String Sayhello () {

Return "Hello 呱";

}

}

}

Please refer to the discussion section of the custom server control above, and no longer repeat.

First import the namespace of the component, instantiate the component defined class, and then use this class to define a good method:

......

USING WAWAComponents;

... ..

l hellocs cscomponent = new wawa_hellocs ();

Label1.text = cscomponent.sayhello ();

In addition to the ways of several multiplexed codes discussed above, the inheritance and web services of the class can also achieve reused code, with inheritance to inherit the members of the parent class, and can override or hide the method and attributes of the parent class as needed. This is a method of multi-purpose multi-use code in object-oriented programming. The web service is a technology that is very hot in recent years, because it can reuse the business logic of others, so that some companies sell their business data, business logic provides new ways, it is based on XML, due to it It is originally invoked, so there will be some losses in performance, but fortunate is to relieve this problem with asynchronous calls and cache. Since inheritance and web service content, I will discuss them in detail in the following discussion.

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

New Post(0)