views:

331

answers:

1

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)
A: 

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

Dan