When you need to use some TemplateField to customize the display, I found that the simplest thing is to first create a GridView control that only contains BoundField, and then add some TemplateField, if necessary, some boundfields can also be directly converted to TemplateField. Now, let's start this tutorial. First, add a GridView control to the page through the designer and bind an ObjectDataSource that returns employee information to it. These steps create a GridView with some boundfields, which correspond to different fields in the employee information.
Open GridViewTemplateField. aspx and drag a GridView from the toolbox to the designer. Select and add a new ObjectDataSource control that calls the GetEmployees () method of the EmployeesBLL class from the smart tag of the GridView.
Data Binding in the GridView: Add a new ObjectDataSource control that calls the GetEmployees () method.
In this way, the data bound to the GridView will automatically add a BoundField for each attribute of the employee information: EmployeeID, LastName, FirstName, Title, HireDate, ReportsTo, and Country. In this report, we do not want to see the attributes of EmployeeID, ReportsTo, and Country. To delete these boundfields, you can:
· Use field dialog box-click "Edit Columns" in the displayed menu of the intelligent Tab Of The GridView ). Then, select the BoundField you want to delete in the list in the lower left corner and click the Red Cross button to delete the BoundField.
· Manually edit the declaration statement of the GridView-in the Source view), find the BoundField you want to delete, which is the <asp: BoundField> element, and delete it.
After you delete BoundField such as EmployeeID, ReportsTo, and Country, the markup language code of your GridView should look like this:
- < asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
- DataSourceID="ObjectDataSource1">
- < Columns>
- < asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
- < asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
- < asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
- < asp:BoundField DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate" />
- < /Columns>
- < /asp:GridView>
Let's take a moment to look at our results in the browser. At this time, you will see a table where every record in the table is an employee's information. There are four columns: one is the employee's surname, the other is the name, and the other is the title, another is their employment date.
Data bound to the GridView: each employee information displays LastName, FirstName, Title, and HireDate
- Use ASP. NET DetailsView to display details
- Familiar with the use of DropDownList and ObjectDataSource
- ASP. NET DropDownList creation and filling
- Use the GridView and ObjectDataSource in ASP. NET
- Example of ASP. NET DropDownList