Frequent DataGrid error

zhaozj2021-02-16  56

Summary: Learn how to avoid some common errors that may happen when using the ASP.NET DataGrid control (this article contains links to English sites).

The DataGrid control is one of the most powerful 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 ("

")

Response.write (MyDataReader (0))

Response.write ("")

Response.write ("")

Loop

Response.write ("")

You can simplify the above code to make it only:

, and calls the .database. 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.

Bindgrid ()

END IF

End 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 using attribute generators in graphical ways.

Note: If the setting of AutoGenerateColumns is kept as "true", and the columns are specified in the DataGrid segment, the repeated settings of the column will eventually be obtained. The system will first display the column of special declarations, followed by all automatically generated columns. Try only using the control ID to reference the controls in the DataGrid project

Many people have not realized that for controls in ItemTemplate under DataGrid (such as TextBox control with "MyTextBox", you cannot use the following code or in the