Form Control Creation

Source: Internet
Author: User

I remember that when I was in college, I learned the concept of Multimedia: Intra-frame compression and inter-frame compression. I feel that the method I mentioned in my first article is a bit like intra-frame compression. Does it reduce the code to the extreme?

The code for adding a single table seems to be, but a project does not have only one page to add. The larger the project, the more pages to add, it is still annoying to write so much code on every page. So what should we do?

Many people think of code generators. Yes, the code generator can reduce our labor, but it cannot reduce code!

On the contrary, the use of code generators limits our imagination and puts us in the status quo. It is inevitable to think that so many codes are normal. There is a code generator anyway, if we are not tired, we will not think of a better solution.

So how can we reduce the code when there are more pages added? Inter-frame compression, that's him!

Everyone who writes code generators knows the functions of these tables in ms SQL. Most of the generated code also uses these tables, but only the code is generated, is it a pity that I didn't think about other functions?

Let's talk about these tables, sysobjects and syscolumns.
In short, sysobjects stores the table name. Of course, in addition to the table, it also contains other information.
1 select name, ID, xtype
2 from DBO. sysobjects
3 where (xtype = 'U') run this SQL statement in the query analyzer. What do you see? Is it very familiar.

Syscolumns is simply the place where the field is put, of course... same as above.
1 select TBL. Name as tablename, col. Name as colname, T. Name, col. Length
2 from DBO. syscolumns Col inner join
3 DBO. sysobjects TBL on col. ID = TBL. ID inner join
4 DBO. policypes t on col. xtype = T. xtype
5 where (TBL. xtype = n'u ')
Run this SQL statement in the query analyzer. What do you see? Table Name, field name, field type, and field size.

We can make this SQL statement into a view (v_col). The following code will be used.

What can we do with this data source? Many people use it as code generators, and I use it as a simple form control!

Put a DataGrid in. aspx and change the ID to DG. The data source is obtained in the background and bound to DG.
By the way, you need to add a query condition: tablename = 'yourtablename '.

Bind the colname field to the first column of DG, add a template column to the second column, and put a textbox in it.

What do you see when you run the program? A form! Form of all fields in a table! Remember the query statement, tablename = 'yourtablename '. Which table is the form of the table to write.

The function shown here is complete. The following is to save the data!

Write a piece of code to save the data of any table!

Do you remember my code for adding data in the first article? I never saw anything? It is not necessary to take a look.

To add data, two arrays and one table name are required. The first array contains the field name, and the second array contains the user input data. Then, a table name is used.

Now we have this information. The field name is in the first column of DG, the user input data is in the textbox of the second column of DG, and the DG is traversed in the saved event, you can obtain this information.

Finally, get the table name and call Dal. insertdatastr ("t_news", str1, STR); on the data access layer.

To add data from another table, you only need to change the table name!

 

PS:

Someone might say, don't lie here. How simple is form control!

There are many shortcomings:
1. Most fields are in English and can be directly displayed to the user.
2. If the primary key is auto-incrementing, an error occurs during saving because this field cannot be assigned a value.
3. fields are not handled using textbox. You also need to drop down the list box, check box, single-dataset, freetextbox, and other controls. Which line does it have only one textbox ?!
4. What should I do if I want to add verification? What should I do if I want to add instructions? Some fields are not required. What should I do?

5. Others.

These are all deficiencies, but they cannot be used to deny this method. If there are deficiencies, we can improve them! Can Form Controls implement these functions?

I will not write down the specific implementation method. Maybe your method is better than what I use now! I have already said that it may affect your usage :).
 

Related Article

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.