ASP. NET -- The GridView control binds data

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.