Asp.net gridview. findcontrol

Source: Internet
Author: User

Bkjia.com Network ProgrammingI recently used FindControl () when using the GridView and found that I could not Find it in the TemplateField. I searched the internet and found that many of my friends encountered the same problems as I encountered, in addition, the solution is not very good for many people on the Internet. I have summarized it myself:

For Find in RowDataBound, you can use if (e. row. rowType = DataControlRowType. dataRow) to limit the range of the Find, because the Find is found in the HeaderTemplate by default. If the range is not limited and cannot be found in the HeaderTemplate, null is returned naturally, and then an error occurs, the DataRow in the DataControlRowType enumeration determines that it is a data row.

The other is in RowCommand. I found that there are more friends with this problem than the previous one. In RowCommand, if you use the default GridView mode, e. commandArgument automatically defines the Index of the row. In this case, you only need to specify gridview1.Rows [Convert. toInt32 (e. commandArgument)]. findControl ("xxx") is enough, but if it is converted to Template, e. commandArgument does not automatically bind any value. It must be manually bound. You can manually write CommandArgument = "<% # (GridViewRow) Container in <ItemTemplate> </ItemTemplate> ). rowIndex %> ", bind the Index of this row to this e. commandArgument.

. Many beginners do not understand why they want to do this here. In fact, I am also a beginner. Simply put, the rows are based on data loops, and each row has the same control, if you do not specify the row Index, you can simply write gridview1.findcontrol ("xxx"). IDE does not know which control to look for, so you must specify the row number.

 

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.