动态 Gridview 模板和唯一控件(即文本框、标签)ID?
在设计时创建 Gridview 时,您可以创建一个如下所示的模板列:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
在 HTML 中,它将为其指定一个唯一的名称,例如:
<span id="gvSelect_ctl02_Label1">blahblah</span>
然后我可以在后面的代码中引用此标签:
CType(e.Row.FindControl("Label1"), Label)
这是完美的。但当我动态创建 TemplateFields 时,我不知道如何执行此操作。我的“InstantiateIn”中有以下代码:
Dim hl As New HiddenField
hl.ID = "hHidden"
hl.Value = 0
AddHandler hl.DataBinding, AddressOf Me.hl_DataBinding
container.Controls.Add(hl)
这确实创建了一个隐藏控件,每行中的 ID 为 hHidden。但它没有给它像“gvSelect_ctl02_hHidden”这样的唯一ID,它只是“hHidden”。我知道有一些方法可以自己将行号附加到它上面。但我想知道是否有办法让它自动执行此操作。并且仍然允许我引用隐藏字段,例如:
CType(e.Row.FindControl("hHidden"), HiddenField)
When creating a Gridview at design time you can create a template column like this:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And in the HTML it will give it a unique name like:
<span id="gvSelect_ctl02_Label1">blahblah</span>
And I can then reference this label in the code behind by:
CType(e.Row.FindControl("Label1"), Label)
Which is PERFECT. But I can't figure out how to do this when I'm creating TemplateFields Dynamically. I've got the following code in my "InstantiateIn":
Dim hl As New HiddenField
hl.ID = "hHidden"
hl.Value = 0
AddHandler hl.DataBinding, AddressOf Me.hl_DataBinding
container.Controls.Add(hl)
And this DOES create a hidden control with the ID as hHidden in each row. But it doesn't give it the unique ID like "gvSelect_ctl02_hHidden" it's just "hHidden". And I know there are ways to append the row number to it myself. But I was wondering if there was a way for it to do this automatically. And still allowing me to reference the hiddenfield like:
CType(e.Row.FindControl("hHidden"), HiddenField)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃..我自己问题的另一个答案。我在 RowCreated 中寻找名称。我应该在 RowDataBound 事件中寻找它。
它现在可以工作了..现在我做得正确了。
(我可能一次要处理太多事情..):S
Ugh.. another answer to my own question. I was looking for the name in the RowCreated. I should have been looking for it in the RowDataBound event.
it works now.. now that I'm doing it correctly.
(I may have too many things on the go at once..) :S