Finding user control in TemplateField of DetailsView

寵の児 提交于 2019-11-29 15:43:48

After DataBinding the control, you'd use:

dvDetailsView.Rows[0].Cells[0].FindControl("ucUserControl")

And make sure you are doing this only in Edit mode as the control only exists in EditItemTemplate.

Khanzor

As it turns out, the user control name was changed - my usercontrol, labelled as "ucUserControl" had it's name changed to a generic name - 'ctl01'.

So, doing advSituation.Rows[0].Cells[0].FindControl("ctl01") found the control.

To find this ID, I just had a look at the HTML element being rendered, and checked the parent from the id, e.g. 'ctl00_MainContent_dvDetailsView_ctl01_lblLabel', where lblLabel appeared on ucUserControl.

The rows column is a 0 based index of the number of fields, and the cells index will be 1 if you have a headertemplate specified.

EDIT: OMG! Someone (it really wasn't me, I swear) had hidden the ID property on the control class!

public partial class UserControl : BaseControl
{
  public int Id;
}

This meant that when ASP.Net was generating the id, it couldn't, and just assigned a generic Id ('ctl01' in this case) to the control, rather than the actual name.

Wow.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!