ASP. NET -- The GridView control binds data
ASP. NET provides many data server controls for displaying table data in a database on a Web page. The GridView control is one of them. This control is almost the same as the DataGridView control we have learned before, so we are no stranger to the GridView control. The following describes how to use it.
Front-end:
Find the GridView control in the toolbox and drag it to the code editing area.
Step 1: Go to the design page. There is a black triangle to the right above the GridView control. Click this button and select Edit column,
Step 2: remove the check box before the automatically generated field and select BoundField Based on the bound content.
Step 3: Set the title name HeaderText and the bound field name DaTaField.
Step 4: If you want to set a uniform style for each row of the form, select to convert each field to TemplateField.
After these steps are configured, HTML code is automatically generated.
Latest news
'> [<% # Eval ("name") %>]
'Target = "_ blank" title = '<% # Eval ("title") %>'> <% # StringTruncat (Eval ("title "). toString (), 18 ,"... ") %>
'>
Hot News
'> [<% # Eval ("name") %>]
'Target = "_ blank" title = '<% # Eval ("title") %>'> <% # StringTruncat (Eval ("title "). toString (), 18 ,"... ") %>
'>
Background code:
Protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {// when the page is accessed for the first time, NewsManager nm = new NewsManager (); // bind the latest news gvNewNews. dataSource = nm. selectNewNews (); gvNewNews. dataBind (); // bind to hot news gvHotNews. dataSource = nm. selectHotNews (); gvHotNews. dataBind ();}}
Comparison:
In the niugu news publishing system, we also talked about a data binding control called the Repeater control. The Repeater control does not have its own built-in rendering function, this means that you must create a template to provide the layout of the Repeater control. You need to use ItemTemplate to define the template.
Compared with the Repeater control, the GridView control is more powerful and flexible, and can customize the style of each column in each row. However, a bad drawback is that there are too many redundant code to be generated.