Obtain the text value displayed by templatefield in the gridview.

Source: Internet
Author: User

In the gridview, I usually adopt two methods to bind the property after the data source is bound.

One is boundfield. You only need to set the corresponding attribute name of datafield;

For example:

<Asp: boundfield headertext = "series" datafield = "Catena" itemstyle-width = "10%"/>

Another is templatefield, which is used to process special displays;

For example:

<Asp: templatefield headertext = "annual sales">
<Itemtemplate>
<% # Getsalesvolumes (container. dataitem) %>
</Itemtemplate>
<Itemstyle width = "10%" horizontalalign = "center"/>
</ASP: templatefield>

However, some events bound to rowdatabound often fail to obtain the text value in templatefield;

The value of row. cells [Index]. Text is "";

Later I checked some information and thought about it. I found that templatefield is a container. It is a sub-container with a custom display value;

Debugging, monitoring, and Discovery

<Itemtemplate>
<% # Getsalesvolumes (container. dataitem) %>
</Itemtemplate>
Row. cells [Index]. Controls. Count = 1 in the write method; I found that the data type is databoundliteralcontrol;

Therefore, the method for getting the value of the Column cell is as follows:

(Databoundliteralcontrol) Row. cells [Index]. controls [0]). Text. Trim ();

If the templatefield contains multiple child controls, obtain the controls (type: controlcollection) of cells [Index] to obtain the corresponding child controls cyclically and perform corresponding operations;

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.