Post) how to obtain the values in the DataGrid binding column and template Column

Source: Internet
Author: User
Sometimes you want to get a value that is not in the editing status. In this case, textbox is usually placed in the template column through E. Item. findcontrol. Why can't some netizens ask? This depends on how the columns on your front-end are written. The following describes four cases and the following value methods: 1 < ASP: DataGrid ID = "Datagrid1" Runat = "Server" Autogeneratecolumns = "False" Allowpaging = "True" >  
2 < Columns >  
3 < ASP: boundcolumn Datafield = "Vname" Headertext = "Name 0" > </ ASP: boundcolumn >  
4 < ASP: templatecolumn Headertext = "Name 1" >  
5 < Itemtemplate >  
6 Name
7 </ Itemtemplate >  
8 </ ASP: templatecolumn >  
9 < ASP: templatecolumn Headertext = "Name 2" >  
10 < Itemtemplate >  
11 <% # Databinder.Eval(Container. dataitem,"Vname") %>  
12 </ Itemtemplate >  
13 </ ASP: templatecolumn >  
14 < ASP: templatecolumn Headertext = "Name 3" >  
15 < Itemtemplate >  
16 < ASP: Label ID = "Name" Runat = "Server" Text = '<% # Databinder. eval (container. dataitem, "vname") % > '>
17 </ ASP: Label >  
18 </ Itemtemplate >  
19 </ ASP: templatecolumn >  
20 < ASP: buttoncolumn Text = "Delete" Buttontype = "Pushbutton" Commandname = "Del" > </ ASP: buttoncolumn >  
21 </ Columns >  
22 </ ASP: DataGrid >  

Lists four types of columns that you may encounter:
(1) bind Columns
(2) directly put static strings in the template Column
(3) directly include the bound string in the template Column
(4) Put the bound string in the template column using the label

The following describes the four values after the delete button is pressed:

1 If (E. commandname = " Del " )
2 {
3 Response. Write (maid [ 0 ]. Headertext + " : " + E. Item. cells [ 0 ]. Text + " <Br> " );
4 Response. Write (maid [ 1 ]. Headertext + " : " + E. Item. cells [ 1 ]. Text. Trim () + " <Br> " );
5 Response. Write (maid [ 2 ]. Headertext + " : " + (Databoundliteralcontrol) E. Item. cells [ 2 ]. Controls [ 0 ]). Text. Trim () + " <Br> " );
6 Response. Write (maid [ 3 ]. Headertext + " : " + (Label) E. Item. cells [ 3 ]. Findcontrol ( " Name " ). Text + " <Br> " );
7 }  

(1) first, you can bind columns directly through cells [I]. text
(2) the second method can also be used directly, but the annoying vs.net always wrap the content in the column at the front end, therefore, we also need to remove spaces at the beginning and end of the output.
(3) the third method cannot directly use cells [I]. text to get the value, you can use the method given above to get, also need to remove spaces at the beginning and end
(4) The fourth method is the simplest way, of course, you can directly write e. item. findcontrol ("name")

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.