我有我回发一个DetailsView - 而那里面是一个用户控件。我遇到一些困难,找到它的回传数据。
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.
作为一个例子:
<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??
编辑:原来,我的用户ID已被改变 - 但为什么呢?我做对的插入和编辑模板,这两个相同的ID,但评论说出来没有什么区别。
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.
事实证明,用户控件的名称已更改 - 我的用户,标记为ucUserControl有它的名字改成一个通用的名字 - 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'.
那么,做了 dvSituation.Rows [0] .Cells [0] .FindControl(ctl01)
找到了控制。
So, doing advSituation.Rows[0].Cells[0].FindControl("ctl01")
found the control.
要找到这个ID,我只是来看看HTML元素被渲染,并从ID,如检查父 ctl00_MainContent_dvDetailsView_ctl01_lblLabel
,其中lblLabel出现在ucUserControl。
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.
的行列是字段的数量的0的索引,并将细胞索引将为1,如果你有一个HeaderTemplate中指定。
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.
修改:OMG!有人(真不是我的,我发誓)所藏的ID属性上的控制类!
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;
}
这意味着,当被ASP.Net产生的ID,它不能,刚分配的通用标识('ctl01'在这种情况下)到控制,而不是实际的名称。
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.
哇。