(Forum questions and answers) Sometimes the DataGrid takes the value not to obtain?

Source: Internet
Author: User
Tags eval trim
Datagrid

Sometimes you want to take a value that is not an edit state, this time usually does not use the template column to place the textbox through the E.item.findcontrol value, some users ask why not? It depends on how your front desk column is written, and here are four different scenarios and the following values in each of these four situations:

<asp:datagrid id= "DATAGRID1" runat= "Server" autogeneratecolumns= "False" allowpaging= "True" >
<Columns>
<asp:boundcolumn datafield= "VName" headertext= "name 0" ></asp:BoundColumn>
<asp:templatecolumn headertext= "Name 1" >
<ItemTemplate>
Name
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Name 2" >
<ItemTemplate>
<% #DataBinder. Eval (Container.DataItem, "VName")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Name 3" >
<ItemTemplate>
<asp:label id= "name" runat= "Server" text= ' <% #DataBinder. Eval (Container.DataItem, "VName")%> ' >
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:buttoncolumn text= "Delete" buttontype= "pushbutton" Commandname= "del" ></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Lists 4 types of columns that you might encounter:
(1) Bound columns
(2) A static string is placed directly in the template column
(3) The string in the template column that binds directly
(4) A string that is bound by a label in the template column

The following is a description of the four values that are taken after the delete button is pressed:
if (e.commandname== "Del")
{
Response.Write (Datagrid1.columns[0]. headertext+ ":" +e.item.cells[0]. text+ "<br>");
Response.Write (Datagrid1.columns[1]. headertext+ ":" +e.item.cells[1]. Text.trim () + "<br>");
Response.Write (Datagrid1.columns[2]. headertext+ ":" + ((DataBoundLiteralControl) e.item.cells[2]. Controls[0]). Text.trim () + "<br>");
Response.Write (Datagrid1.columns[3]. headertext+ ":" + (Label) e.item.cells[3]. FindControl ("name")). text+ "<br>");
}
(1) The first type of binding column can be directly passed through Cells[i]. Text Fetch
(2) The second way can also be directly taken, but the annoying vs.net will always be in the foreground of the contents of the column line, so here also need to export things to remove the end of the space
(3) The third Way is to ask the most users, can not directly through the cells[i]. Text to the value, can be given by the above method to take, also need to remove the end of the space
(4) The fourth Way is also the simplest way, of course you can write directly 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.