Marcie RobillarddataGridgirl.com
November 2003
Applicable to: Microsoft® ASP.NET
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 create list data using DataGrid 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 (" td>")
Response.write (" TR>")
Loop
Response.write (" Table>")
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.
Forget one of the most common errors in ISPostback in the Page_Load event 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 column. If the environment where the DataGrid is required, you must shut down 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 columns> 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 control in the DataGrid project Many people do not realize that for the controls in the itemtemplate under the TemplateColumn of DataGrid (such as the TextBox control with "MyTextBox", you cannot follow the following code or in ASPX This control is called directly in the |