Find a control in datagrid template column

gedaba

Newcomer
Joined
Feb 26, 2004
Messages
3
I need to find the control "btnSort1" that is defined as follows

<asp:TemplateColumn Visible="False">
<HeaderTemplate>
Title
<asp:ImageButton id="btnSort1" runat="server" CommandName="sort" ImageUrl="../images/arrowdown.gif" CommandArgument="nvcTitle" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.nvcTitle") %>' ID="lblTitle"/>
</ItemTemplate>
</asp:TemplateColumn>


The code behind is:

Dim itm As DataGridItem
Dim ctrl As Control

For Each itm In grdFriends.Items
For Each ctrl In itm.Controls
If Not IsNothing(ctrl.FindControl("btnSort1")) Then
CType(ctrl.FindControl("btnSort1"), ImageButton).ImageUrl = "../images/arrowdownred.gif"
Else
Dim ctrl2 As Control
For Each ctrl2 In ctrl.Controls
If Not IsNothing(ctrl2.FindControl("btnSort1")) Then
CType(ctrl2, ImageButton).ImageUrl = "../images/arrowdownred.gif"
Else
Console.WriteLine(ctrl2.ID)
End If
Next
End If
Next
Next


the code doesn't break or crash but I can't find the item and instead I can find find lblTitle in the second loop ctrl2.control("lblTitle")

Can I search for a control that is stored in the header template? If yes, How?
 
Code:
Dim datagriditem As DataGridItem
Dim txtBox As TextBox 'Or any other control

For Each datagriditem In DataGrid1.Items
   If datagriditem.ItemType = ListItemType.Item Or datagriditem.ItemType = ListItemType.AlternatingItemThen
      txtBox = CType(datagriditem.FindControl("txtMyControl"), TextBox)
      txtBox.Text = "Test"
   End If
Next


Or you can do this in the ItemDatabound event
 
Thanks,

that works fine if the control I want is a listitem or a rowitem. I want to change an image that I added on the header template i.e.

<HeaderTemplate>
Title
<asp:ImageButton id="btnSort1" runat="server" CommandName="sort" ImageUrl="../images/arrowdown.gif" CommandArgument="nvcTitle" />
</HeaderTemplate>

I want to change :

ImageUrl="../images/arrowdown.gif" to ImageUrl="../images/arrowup.gif"

is that possible? if yes, how?
 
As you are going through the items of the datagrid, you are checking what each type it is before you take action...
Code:
If datagriditem.ItemType = ListItemType.Item Then

End If

Change ListItemType.Item to whatever you want to check for. ie. item, edititem, header, footer, alternatingitem.
 
even i wanted to know the same think
how to change an image when clicked in the header
please help
its very urgent
 
Back
Top