在DetailsView的TemplateField中查找用户控件
我有一个要发回的详细信息视图,其中有一个用户控件。 我在回发数据中找到它时遇到一些困难。
举个例子:
<asp:DetailsView ID="dvDetailsView" runat="Server" AutoGenerateRows="false">
<Fields>
<asp:TemplateField>
<ItemTemplate>
Some text here
</ItemTemplate>
<EditItemTemplate>
<uc:UserControl ID="ucUserControl" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<uc:UserControl ID="ucUserControl" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
当我回发时,我会假设我会做这样的事情:
MyUserControlType ucUserControl = dvDetailsView.FindControl("ucUserControl") as MyUserControlType;
但这什么也没发现。 事实上,我什至在 QuickWatch 中走来走去都找不到这个宝贝……
我需要做什么才能找到这个东西?
编辑:事实证明我的用户控件 ID 已被更改 - 但为什么呢? 我在插入和编辑模板上确实有相同的 ID,但注释掉它没有什么区别。
I have a DetailsView that I'm posting back - and inside of that is a UserControl. I'm having some difficulty located it in the postback data.
As an example:
<asp:DetailsView ID="dvDetailsView" runat="Server" AutoGenerateRows="false">
<Fields>
<asp:TemplateField>
<ItemTemplate>
Some text here
</ItemTemplate>
<EditItemTemplate>
<uc:UserControl ID="ucUserControl" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<uc:UserControl ID="ucUserControl" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
When I postback, I would assume I would do something like this:
MyUserControlType ucUserControl = dvDetailsView.FindControl("ucUserControl") as MyUserControlType;
But this finds nothing. In fact, I can't even find this baby by walking around in QuickWatch...
What do I need to do to find this thing??
EDIT: It turns out my usercontrol id was being changed - but why? I do have the same ID on both the insert and edit templates, but commenting that out made no difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
DataBind
控件之后,您将使用:并确保仅在编辑模式下执行此操作,因为该控件仅存在于
EditItemTemplate
中。After
DataBind
ing the control, you'd use:And make sure you are doing this only in Edit mode as the control only exists in
EditItemTemplate
.事实证明,用户控件名称已更改 - 我的用户控件,标记为“ucUserControl”,其名称已更改为通用名称 - 'ctl01'。
因此,执行
dvSituation.Rows[0].Cells[0].FindControl("ctl01")
找到了控件。为了找到这个 ID,我只是查看了正在呈现的 HTML 元素,并从 id 检查了父元素,例如“ctl00_MainContent_dvDetailsView_ctl01_lblLabel”,其中 lblLabel 出现在 ucUserControl 上。
行列是字段数量的基于 0 的索引,如果指定了标题模板,则单元格索引将为 1。
编辑:天啊! 有人(我发誓这真的不是我)隐藏了控件类的 ID 属性!
这意味着当 ASP.Net 生成 id 时,它无法生成该 id,而只是为该控件分配了一个通用 Id(在本例中为“ctl01”),而不是实际名称。
哇。
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 a
dvSituation.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!
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.