ASP. NET development, editing and modifying, and distinguishing responsibilities

Source: Internet
Author: User

This article describes how to use Insus. NET to compile and modify a function in ASP. NET development. In the case of changing methods and multiple forms, how to write the program is simple and clear.

 
The following is an Excel document:
Http://download.cnblogs.com/insus/ASPDOTNET/Book1.rar

 

Now is to read it and display it on the GridView control, the method reference Excel upload and read data http://www.cnblogs.com/insus/p/3814857.html because some fields in this article, useless, so delete it.

"SELECT [gender], [Department], [position], [Employment No] FROM [Sheet1 $]"

 
We are in html markup of xxx. aspx. Pull a GridView control and use the Custom template:

 

Read Excel Demo:

 

Now, you need to read and load the Excel file to the GridView and want to edit the data. However, you do not want to enter the editing status only after you click the button on one line in the same way as in the traditional mode. You can edit the button at the beginning. There are only two values for editing: "gender" and "active/standby. "Male" and "female"; "active" and "resigned" can use the RadioButtonList control. The "department" and "position" in the two fields are displayed with multiple values. You can use the DropDownList control:

 

Since there are controls, we need to use the data source to bind the control data. Since these controls are in the GridView control, they are bound to the OnRowDataBound event.

 

Bind data of four controls nested in the GridView control:

 


In the preceding code example, Insus. NET only provides two widgets, one DropDownList and one RadioButtonList. Their Data Sources come from databases and functions. You need to specify the displayed data (DataTextFeid), which is used as the data obtained by the backend (DataValueField ). This depends on your development needs.


Here, you find that four controls have code redundancy when binding data sources:

 ccc.DataSource = xxx; ccc.DataTextField = yyy; ccc.DataValueField = zzz; ccc.DataBind();


We can refactor it into a method. before restructuring, we will encounter two problems to consider. One is that the control is different. RadioButtonList and DropDownList; second, the bound data sources are different, some of which are generic and able. To handle this, you need to come up with a 10 thousand strategy. For example:

 

The two Red Arrow headers are displayed. Because the RadioButtonList and DropDownList controls inherit the ListControl controls. The data source is declared as an object. Then bind the four controls above to use this method:


If I remove the commented-out code, isn't it much more concise? Check the running browsing result:

 


Now we can see a problem. The two DropDownList drop-down lists do not have the "-- select --" option. How to add the first item? Oh, this is simple:


Add the above two lines of highlighted code to achieve the following results:

 

So far, we have only bound the data source of the control. Let's look back at the downloaded Excel Data:

Correctly, it should be to bind the original data status together, OK, solve this problem, and then write the program:

Match, match, and change the status of each record and the data bound to the control.
View the results:


Well, it's getting closer and closer to the ideal requirements. Development is like this. A function, a method, or a function can distinguish responsibility.
The above code is redundant again:

 foreach (ListItem li in ccc.Items)                    li.Selected = li.Text == drv["xxx"].ToString() ? true : false;


In the development process of Insus. NET, once code redundancy is found, it must be reconstructed, otherwise it will not look comfortable. Create a method:

For this method, you can also refer to another article "nothing, development of ASP. NET casually write, think of what to write what" http://www.cnblogs.com/insus/p/3810032.html will learn a better way to refactor.

After writing this public method, we will rewrite the redundant code:

 

Write the program in one step ......

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.