Accessing label inside headertemplates

  • Thread starter Kristoffer Arfvidson
  • Start date
K

Kristoffer Arfvidson

<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" Font-Names="Verdana, Arial, Helvetica,
sans-serif"
AllowCustomPaging="True" GridLines="Horizontal">
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="Teal"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<b><asp:label id="lblHeadline" Runat="server"></asp:label></b>
</HeaderTemplate>
<ItemTemplate>


and so on...
HOW???
Can I access lblHeadline from codebehind?

I got some answer before that you should use Findcontrol...
I can believe that this is the right answer, but I simply can´t find that
control...
I tryed:

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim tmpCtrl As Object
'tmpCtrl = e.Item.FindControl("lblHeadline")
tmpCtrl = DataGrid1.FindControl("lblHeadline")
tmpCtrl .Text = "HELLO!"
End Sub

but, I simply can not find the damn thing...
I believe that the reason is that it is lying behind a asp:templatecolumn,
but I tryed to put an id on that but my VS simply dont want me to... :(

Any suggestions on how to find this label?

Yours: Kristoffer
 
K

Kristoffer Arfvidson

FOUND IT!!!

Never mind, my question I found it...

I did it by using the following code...

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If (e.Item.ItemType = ListItemType.Header) Then
Dim i As Integer
For i = 0 To e.Item.Controls.Count - 1
Try
If (e.Item.Controls(i).Controls(1).GetType().ToString()
= "System.Web.UI.WebControls.Label") Then
Dim tb As Label
tb = e.Item.Controls(i).Controls(1)
tb.Text = "TJABBA!"
End If
Catch

End Try
Next
End If
End Sub

Maybe not the best code, and especially if you have multiple controls, but I
only have one, so this works for me for now, if you guys have any better
suggestions, please share...

Yours: Kristoffer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top