Summary of ASP. NET learning by old cat

Source: Internet
Author: User
I. Processing order of ASP. NET pages

ASP. NET pages are event-driven. The first thing to execute is the init event.CodeThe system automatically adds an oninit overload function to execute this oninit function during page init. In this function, the initializecomponent function is executed. There are many control event proxy functions in this function. Expand the "Web Form Designer generated code" Page and we can see these.
After executing init, the page is the load event, and the corresponding code is page_load.
The code that causes the event returned by the page. Of course, if the page is running for the first time, this step will not be taken.
That is to say, the page_load function is executed every time the page is refreshed, which is why some code should be written in

Program Code: If (! Page. ispostback)
{...}

. For example, if you set an initial value in page_load in a text box, where is it stored? If it is placed outside the if clause, the value assignment statement will be executed every time you refresh the page. Even if the value of the text box is modified on the page, the value will disappear after the result is returned, but if it is placed in the IF clause, then, the returned value is still the previous value in the text box.
Many friends cannot obtain the edited value in the DataGrid, which may be the cause.

II. Application of JavaScript on pages

although ASP. NET adopts a new operating mechanism, the generated HTML and JavaScript code can still be recognized by General browsers.
on the page, you can still use response. write to write a piece of JS Code. For example, to pop up a new browser window, you can use response. write (@ " ");
in the web control, the attributes attribute set is also closely related to Js. The newly added attributes element corresponds to an attribute or event of the HTML code generated by the web control.
for example, if there is a textbox that requires us to select all the text when it gets the focus, we can handle it like this: textbox1.attributes ["onfocus"] = "javascript: This. select (); "; by viewing the Source Code On the IE page, we can see the following section: , so I don't need to explain what it means.
there is another application. If there is a button, its click event contains many operations on the database, we ask the user to confirm it once after the button is pressed (this is a bit like msgbox in ASP). Now we can handle it like this: button1.attributes ["onclick"] = "javascript: return confirm ('are your sure... '); "; the final effect is as we imagined.
Note: The above code passes through codebehind. The code and page may be different in writing.

Iii. code reuse

ASP. NET has basically switched to the OO level, and the code is basically executed through classes. Just like writing a C/S system, we use one or more classes to save common functions and then call them in other functions.
Another method is to write it to a base class. Other classes inherit this base class. As for the method, it depends on your hobbies and the scope of function processing.
A project I created in the previous event was implemented in this way. There are two base classes, one is the base class of the middle layer, and the other is the base class of the page class (Inheriting System. web. UI. page ). Then, some databases are processed in the previous base class, and some common operations of page controls are put in the next base class.

Iv. Web controls

We should have a deep understanding of several controls: DataGrid and datalist. These two controls provide us with great convenience in displaying data. Their usage requires us to constantly explore and summarize them in practice.
In addition, custom controls are also an important scope.
In terms of the Type Selection of custom controls, if you only display some inherent controls without attribute or event operations, writing them as user control is simpler, such a custom control is equivalent to the Code executed through include in ASP. If you want to modify the internal properties of the control and obtain some events of the control during design and operation, you 'd better write a DLL file, that is, write a class, such controls can be like General Web controls, such as dragging the size with the mouse, setting properties, and obtaining events.

Allows the ASP. NET Server Control to issue client script blocks in the page.

[Visual Basic]

Program code: overridable public sub registerclientscriptblock (_
Byval key as string ,_
Byval script as string _
)

[C #]

Program code: Public Virtual void registerclientscriptblock (
String key,
String script
);

[C ++]

Program code: public: Virtual void registerclientscriptblock (
String * key,
String * script
);

[JScript]

Program code: Public Function registerclientscriptblock (
Key: string,
Script: String
);

Parameter remarks
The unique key that identifies the script block.
The content of the script sent to the client.

The client script is issued immediately after the start mark of the <form runat = Server> element of the page object. The output object is defined when a script block is generated. Therefore, it must contain two tags of the <SCRIPT> element.

By using the keyword to identify the script, multiple server control instances can request the script block instead of sending it to the output stream twice.

Any script block with the same key value is considered repeated.

Note: Remember to add the HTML annotation mark around the script so that the script will not be rendered when the requested browser does not support the script.

Example
[Visual Basic, C #] the following example shows how to use the registerclientscriptblock method with isclientscriptblockregistered. If you have not yet registered ecmascript written in the Code declaration block (such as the isclientscriptblockregistered test), a registerclientscriptblock call will be issued.

[Visual Basic]

Program code: <HTML>
<Head>
<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)

'Form the script that is to be registered at client side.
Dim scriptstring as string = "<script language = JavaScript> function doclick (){"
Scriptstring + = "myform. Show. value = 'Welcome to Microsoft. net'} <"
Scriptstring + = "/"
Scriptstring + = "script>"

If (not isclientscriptblockregistered ("clientscript "))
Registerclientscriptblock ("clientscript", scriptstring)
End if
End sub
</SCRIPT>
</Head>
<Body topmargin = "20" leftmargin = "10">
<Form ID = "myform" runat = "server">
<Input type = "text" id = "show" style = "width = 200">
<Input type = "button" value = "clickme" onclick = "doclick ()">
</Form>
</Body>
</Html>

[C #] 

Program code: <HTML>
<Head>
<Script language = "C #" runat = "server">

Public void page_load (Object sender, eventargs e ){

// Form the script that is to be registered at client side.
String scriptstring = "<script language = JavaScript> function doclick (){";
Scriptstring + = "myform. Show. value = 'Welcome to Microsoft. net'} <";
Scriptstring + = "/";
Scriptstring + = "script> ";

If (! This. isclientscriptblockregistered ("clientscript "))
This. registerclientscriptblock ("clientscript", scriptstring );
}

</SCRIPT>
</Head>
<Body topmargin = "20" leftmargin = "10">
<Form ID = "myform" runat = "server">
<Input type = "text" id = "show" style = "width = 200"> <input type = "button" value = "clickme" onclick = "doclick ()">
</Form>
</Body>
</Html>

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.