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.