DataGrid in ASP.NET (1)

zhaozj2021-02-16  68

Summary: How to avoid some common errors that may happen when using the ASP.NET DataGrid control.

The DataGrid control is one of the most functional and useful web controls in Microsoft® ASP.NET, which has been recognized by ASP.NET authoritative. Although the DataGrid control is easy to use, it is also easy to bring trouble to the user. The following is some mistakes made by many people, including from beginners to experience .NET experts. You can see that many depressed users have issued a problem in the ASP.NET newsgroup and forum. Follow the fairly simple steps overview of this article, you can help you avoid these errors and save a lot of development time.

You can use DataGrid to create list data without using

I know that you will not use the code as follows, but many old users in the ASP.NET field continue to use them:

Response.write ("

") While MyDataReader.Read () Response.write ("
") Response.write (MyDataReader (0)) response.write ("" ") Response.write (" ") loopResponse.write (" ")

You can simplify the above code to make it only:

And call the .dataBind () method. Even if you need to perform special controls for HTML output, you can use a data web control if you can repeat the content of the set on the user interface.

Forgot to check ISPOSTBACK in Page_Load event

One of the most common errors is to check the ISPostBack condition of the page before the data binding. For example, when the DataGrid is in "Edit" mode, ignore the item will result in the original value of the edited value by the original value in the data source. However, this rule has at least one of the main exceptions, see Continuing to use large viewState.

The following is a typical Page_Load event containing the ISPostBack check. Bindgrid () is a routine for importing and setting the data source of the DataGrid, and calls the DATABIND () method.

SUB Page_load if not ispostback the bindgrid () end ifend sub

When you need more flexibility, you still insist on using the automatically generated columns.

If the environment in which the DataGrid needs any special format, or any other web control in the DataGrid, you must close AutoGenerateColumns. Keep the settings of the AutoGenerateColumns property to "True" practice, which is only valid in the simplest DataGrid scheme. However, for almost all actual applications, this property must be set to "false" and specify the column in the segment declared by the DataGrid. Microsoft Visual Studio? NET users can create these columns in graphical ways using attribute builders.

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.049, SQL: 9