Grid implementation developed by Asp. Net Server controls (1), asp. netgrid

Source: Internet
Author: User

Grid implementation developed by Asp. Net Server controls (1), asp. netgrid

When Asp. Net is used for Web development, the control provided by the system is sometimes difficult to achieve the desired goal. At this time, there are multiple ways to solve the problem, such as using html + js to layout the desired interface on the front end, and then obtaining data through ajax to achieve the goal.

However, this is very difficult, especially when the layout of the interface needs to be reused, it is more difficult. Some people will directly copy the code on the interface, but this will inevitably cause greater problems for later maintenance. Therefore, there is no way to reuse and maintain the layout. Asp. Net controls can be easily laid out, and controls can be directly operated on the background to implement relevant data logic. So I want to use asp.net technology to encapsulate controls from rows.

Some people will say that the use of asp.net's user-defined control encapsulation can not achieve the goal? To a certain extent, it does. However, if we need more features, it is still difficult to meet the requirements. Especially when we use some libraries on the Mobile End for development, the problem will be more serious.

For example, when JqueryMobile is used for development on the Mobile End, JqueryMobile is based on the frontend, so it may be tricky to use the backend. In this case, we can consider asp.net + jquerymobile. (Use asp.net mvc4 to combine the non-attribute range of jquerymobile)

We implement a Grid control through simple encapsulation. Take a look.


The Code is as follows:

Front-end code default. aspx

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "default. aspx. cs" Inherits = "Demo. _ default" %> <! DOCTYPE html> The control with the S prefix is the encapsulated Grid control.

The following code defaults. aspx. cs

Using System; using System. collections. generic; using System. data; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace Demo {public partial class _ default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {InitLoad () ;}} private void InitLoad () {Grid_Edit.DataSource = GenerateData ();} private DataTable GenerateData () {DataTable dt = new DataTable (); dt. columns. add ("NO"); dt. columns. add ("Type"); dt. columns. add ("Status"); dt. rows. add (new String [] {"H10001", "food", "sold out"}); dt. rows. add (new String [] {"H10002", "Vegetables", "to be Sold"}); dt. rows. add (new String [] {"H10003", "Fruit", "to be Sold"}); dt. rows. add (new String [] {"H10004", "appliance", "sales"}); return dt ;}}}
Assign a value to the DataSource of Grid_Edit.

From the code, we can see that S: Grid has a column in it, and there is also the S: BoundField field in Columns, which has the HeaderText and DataField attributes in the S: BoundField field.

HeaderText is the column header, and DataField is the bound field, which corresponds to the DataSource field of the background code.

This method is similar to Asp. Net's GridView.

Therefore, to encapsulate a Grid, we must at least do the following:

1. Define a Grid class

2. the Grid class contains Columns attributes.

3. Columns attributes must be able to accommodate BoundField fields.

4. You must be able to directly extract <Columns> from the editor.

For more information, seeGrid Implementation of Asp. Net Server Control Development (II)





How to Develop aspnet server controls

1: The user control is very simple. I believe the landlord did not come here to ask this question.
2: If it is a custom control, it may be troublesome. This should be said to belong to another field. The landlord needs to find some information about custom controls on the Internet.
It is difficult to customize controls (such as the Word edit box and paging control). You need to learn this by yourself.
I personally think that General programs can be completely solved by using user controls.
"Some controls of asp.net do not conform to my usage habits. I want to write some controls that suit my habits." Haha, I don't know whether the landlord's habits are good. We recommend that you conform to Microsoft's ideas and habits. This helps you better understand and learn about asp.net.

Lifecycle of server controls in ASPNet

(1) initialization: in this phase, two tasks are completed: 1. initialization of the settings required during the lifecycle of incoming Web requests; 2. Tracking the view status. First, the page framework triggers the Init event by default and calls the OnInit () method. The control developer can override this method to provide the initialization logic for the control. Then, the page Framework calls the TrackViewState method to track the view status. Note that the TrackViewState method provided by the Control base class is sufficient in most cases. Developers can override the TrackViewState method only when the control defines complex attributes.

(2) Load view status: the main task of this phase is to check whether the ASP. NET Server control exists and whether it needs to be restored to the status of the request that ends before processing. Therefore, this process occurs in the page return process, rather than the request initialization process. In this phase, the page framework automatically restores the ViewState dictionary. If the server control does not maintain its state, or it has the ability to save all its States by default and uses the ViewState dictionary, developers do not have to implement any logic. For data types that cannot be stored in the ViewState dictionary or custom State management, developers can rewrite the LoadViewState method to customize state recovery and management.

(3) Process send-back data: To enable the control to check the form data sent back by the client, you must implement the LoadPostData () method of the System. Web. UI. IPostBackDataHandler interface. Therefore, only the controls that process the returned data participate in this phase.

(4) load: At this stage, the ASP. NET Server Control in the control tree has been created and initialized, and its status has been restored. The Form Control reflects the client data. In this case, developers can rewrite the OnLoad () method to implement the common logic of each request.

(5) Send a change notification: At this stage, ASP. the NET Server Control triggers an event as a signal to indicate that the status of the control changes due to sending back (so this stage is only used for sending back ). To establish this signal, developers must use the System. Web. UI. IPostBackDataHandler interface again and implement another method-RaisePostBackChangedEvent (). The determination process is as follows: if the control status is changed due to sending back, LoadPostData () returns true; otherwise, false. The page framework tracks all controls that return true and calls RaisePostDataChangedEvent () on these controls ().

(6) process sending back event: this stage processes the client events that cause sending back. To map client events to server events for processing, developers can implement this logic by implementing the RaisePostBackEvent () method of the System. Web. UI. IPostBackEventHandler interface at this stage. In this way, the server control will successfully capture and send back client events for corresponding processing on the server side.

(7) pre-rendering: this stage completes any work required before the control is generated. Generally, the OnPreRender () method is rewritten to complete the task. Note that changes made to the control status in the pre-rendering phase can be saved in this phase, and changes made in the rendering phase will be lost.

(8) Save status: If ASP. the NET Server control does not maintain the state, or it has the ability to save all its States by default and use the ViewState dictionary, so developers do not have to implement any logic at this stage. Because the process of saving the status is automatic. If the ASP. NET Server control needs to be saved in a custom state, or the control cannot store special data types in the ViewState dictionary, you must rewrite the SaveViewState () method to save the state.

(9) Rendering: indicates the process of writing markup text to the HTTP output stream. Developers rewrite the Render () method to make... the remaining full text>

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.