Obtain the value of a column in the templatefield template of the gridview.

Source: Internet
Author: User

When the gridview control is not added to a template, it is easy to get the value of a row or column.

For example:

E. Row. cells [3]. Text // gets the content in the fourth unit of the currently selected row. Here I put this line of code in the onrowdatabound event handler.

However, if templatefield is added to this column, as follows:

<Asp: templatefield headertext = "item name">
<Edititemtemplate>
<Asp: textbox id = "textbox1" runat = "server" text = '<% # BIND ("modelname") %>'
Width = "85px"> </ASP: textbox>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # BIND ("modelname") %>'> </ASP: Label>
</Itemtemplate>
<Itemstyle horizontalalign = "center" width = "95px"/>
</ASP: templatefield>

At this time, if you use the previous method to obtain the value, the null value will be obtained. In this case, you need to use findcontrol to obtain the value according to the following methods:

 

Label lbl1 = (Label) E. Row. findcontrol ("label1"); // The ID in itemtemplate instead of edititemtemplate
String Ss = lbl1.text;

In this way, the desired value can be obtained correctly.

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.