How do you access controls in the HeaderTemplate of a DataGrid

  • Thread starter Thread starter SamIAm
  • Start date Start date
S

SamIAm

Hi

I have a DataGrid called dgTraders 1 of the columns is a template column and
this column has a Dropdown box in its HeaderTemplate section. The Dropdown
box is called selCities. How do I access this DropDown box at runtime?

<asp:datagrid id="dgTraders" runat="server" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
GridLines="Vertical" ForeColor="Black" AutoGenerateColumns="False"
Width="100%">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="Firstname"
HeaderText="Firstname"></asp:BoundColumn>
<asp:BoundColumn DataField="Lastname"
HeaderText="Lastname"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="City">
<HeaderTemplate>
<asp:DropDownList id="selCities"
runat="server"></asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.City") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.City") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CountryName"
HeaderText="Country"></asp:BoundColumn>
<asp:BoundColumn DataField="SubscriptionStatusName"
HeaderText="Subscription"></asp:BoundColumn>
<asp:ButtonColumn Text="Select"
CommandName="Select"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
 
you can use ItemCreate and ItemDataBound events, find the type of item -
item, header, alternating item etc. - and then use FindControl on that item
i.e. DataGrid row.

--
Regards,
Bipin Joshi
Webmaster - www.dotnetbips.com
Coding, Consulting & Training - www.bipinjoshi.com
Microsoft MVP
 
Back
Top