How do you access controls in the HeaderTemplate of a DataGrid

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default How do you access controls in the HeaderTemplate of a DataGrid

    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>


    SamIAm Guest

  2. Similar Questions and Discussions

    1. How Access Controls In A DataGrid Footer
      OK - I am prepared for everyone who will tell me this is a hack and bad idea; however, this is the only way I could find to solve this problem, and...
    2. Using labels in the HeaderTemplate of a datagrid or datalist
      when I try to access this label to place a text in it in the code. I get this error - Object reference not set to an instance of an object. I...
    3. DataGrid HeaderTemplate Bug?
      I use theDataGrid control and specify a HeaderTemplate for the grid. I the rendered HTML the header row is not rendered as <th> elements. Is this a...
    4. How to access the controls that are inside of a DataList/DataGrid using JS?
      My problem is simple, i have a datalist control with some controls inside of EditTemplate (the problem is the same in the DataGrid). I want to have...
    5. Method to Access Controls In A DataGrid Footer
      Hi, I noticed some ppls are struggling on access Controls In A DataGrid Footer this is the way i done and works pretty well . Take a look...
  3. #2

    Default Re: How do you access controls in the HeaderTemplate of a 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 - [url]www.dotnetbips.com[/url]
    Coding, Consulting & Training - [url]www.bipinjoshi.com[/url]
    Microsoft MVP

    "SamIAm" <samiam@rubbachicken.com> wrote in message
    news:ecuKpG7ODHA.452@TK2MSFTNGP11.phx.gbl...
    > 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>
    >
    >

    Bipin Joshi [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139