How to access a CheckBox in the header row of a Template Column in a data-bound DataGrid


Please help.
How can one get a handle to a CheckBox that is in the header row of template column in a DataGrid that is data-bound?
See below for details on what does NOT work.
Any ideas?
Please advise.
Thank you.

Here is the code in the ASPX file...

<asp:datagrid id="MainDataGrid" runat="server">
<headerstyle horizontalalign="Left" verticalalign="Top" backcolor="LightBlue" font-bold="True" font-size="x-small"></headerstyle>
<itemstyle horizontalalign="Left" verticalalign="Top" backcolor="White" font-size="x-small"></itemstyle>
<alternatingitemstyle horizontalalign="Left" verticalalign="Top" backcolor="Silver" font-size="x-small"></alternatingitemstyle>
<asp:checkbox id="SelectAllForDelete" runat="server" oncheckedchanged="SelectAllForDelete_CheckedChanged" autopostback="True"></asp:checkbox>
<asp:checkbox id="SelectForDelete" runat="server" oncheckedchanged="SelectForDelete_CheckedChanged" autopostback="True"></asp:checkbox>

Here is the code in the ASPX.CS file...

protected void DoIt()
Control thisObjectAsControl;
CheckBox thisObjectAsCheckBox;
//Get a handle to the control.
//Does not work...
//thisObjectAsControl = MainDataGrid.FindControl("SelectAllForDelete");
//Does not work...
//thisObjectAsControl = MainDataGrid.Items[0].FindControl("SelectAllForDelete");
//Does not work...
//this.SelectAllForDelete.Checked = false;

//Does not work...
//thisObjectAsControl = this.FindControl("SelectAllForDelete");
//...assume that we DO have a handle to the control... then...
thisObjectAsCheckBox = (CheckBox)thisObjectAsControl;
//Set the checked value.
thisObjectAsCheckBox.Checked = false;
catch (Exception ex)

And the CheckBox being sought is declared at the class-level in the ASPX.CS file as...

protected System.Web.UI.WebControls.CheckBox SelectAllForDelete;
7/7/2004 5:27:47 PM 91979 articles. 4 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 48


try looping through the datagriditems with a for each loop. If it equals to a headeritemtemplate you know you're in the header. There do a findcontrol in the column for the checkbox and cast it to type checkbox.
Grz, Kris.
Read my blog. Handy Firefox plugins for web developers.

Workaround for non working Mark as answer buttons.
7/7/2004 5:35:36 PM
Hi Kris--

Thank you for the reply.
Unfortunately, I cannot get that to work either.
(Perhaps I misunderstood what you were saying.)
Below is what I tried and the result.
Do you have any other ideas or more details on this one?
Please advise.
Thank you.

string thisType = "";
foreach (DataGridItem thisItem in this.MainDataGrid.Items)

thisType = thisItem.GetType().ToString();
//thisType = ""System.Web.UI.WebControls.DataGridItem" here every time.
7/7/2004 6:38:20 PM

....FYI and BTW, here is something else that does NOT work...

//This does not work...
string thisText = "";
string thisType = "";
Object columnAsObject;
System.Web.UI.WebControls.DataGridColumn columnAsDataGridColumn;
IEnumerator thisEnumerator;
thisEnumerator = this.MainDataGrid.Columns.GetEnumerator();
//This cycles through and finds the columns of type "TemplateColumn"
//but it does NOT give access to FindControl() for that column
//and the Text property is always an empty string so a handle
//to the CheckBox cannot be got with this approach.
while (thisEnumerator.MoveNext())
columnAsObject = thisEnumerator.Current;
columnAsDataGridColumn = (DataGridColumn)columnAsObject;
thisText = columnAsDataGridColumn.HeaderText;
thisType = columnAsDataGridColumn.GetType().ToString();
//columnAsDataGridColumn.FindControl() DNE here....

....still looking for a solution...
7/7/2004 7:00:41 PM

The Items collection only contains items bound to the DataSource, you won't find headers, footers, or seperators here.
7/8/2004 12:53:11 AM
You are going to have to work with the Controls array, I believe.

See my article In Search Of ASP.Net Controls and let me know if it helps. I have a section covering controls in the header / footer of a DataGrid control.
7/8/2004 12:54:55 AM

That is GREAT.
Your article was absolutely correct and the solution you note works perfectly.
Funny. I had actually tried to get a handle to the DataGrid's DataGridTable but had a hard time grabbing it and casting it to an object of that type for manipulation. It looked like DataGridTable objects could not be instantiated stand-alone. As such, I gave up on that tangent. Too bad. Apparently, I should have kept digging. I must say, however, that it is somewhat less-than-intuitive to have the Header/Footer rows tucked away in such a secret comparment; but, I grant that there may be some sense to it somewhere. As ever, finding something is easy once you know where it is.
As a point of closure and completeness relative to my sample code above, a Q & D implemenation of the solution (as provided by Bitmask's fine article) is below, for anyone interested.
That said, the article that Bitmask links above is quite good and is worth a look-see.
Thanks again, Bitmask.

//Quote from Bitmask @ :
//"We know the parent of each DataGridItem is a DataGridTable,
//and the parent of the DataGridTable is the DataGrid control itself.
//We will have to assume the DataGridTable is the first control in the DataGrid, and
//the header row will be the first control of the DataGridTable".
//And so, that leads to the following Q & D code...
CheckBox thisSelectAllForDeleteCheckBox;
thisSelectAllForDeleteCheckBox = MainDataGrid.Controls[0].Controls[0].FindControl("SelectAllForDelete") as CheckBox;
thisSelectAllForDeleteCheckBox.Checked = false;
7/8/2004 4:20:15 PM

Similar Artilces:

display XML data in datagrid with checkbox column <template column>
i can set the datagrid.datasource = myXmlDataset, but anyone know how i can hook an unbound column (template column with a checkbox) to one of my XML fields ??? i was thinking of creating a datatable from scratch, cycling thru the items in the XML document and wring them into the table, then filling myXmlDataset, then binding that dataset to my datagrid. but this has proved to be more of a challenge than i thought. anyone tried doing this before ??? is it even feasible ???"If it is secrect enough, you alone know it." I might be missing something but why don't you read you...

Accessing data in datagrid by row and column.
How can I access a strSection = ddlSection.SelectedValue() For Each d In dgDeadline.Items Dim chk As CheckBox = d.FindControl("chkEnable") Dim key As Guid = dgDeadline.DataKeys(d.ItemIndex) If chk.Checked = True Then Dim sql As String Dim strnewDeadline As DateTime strnewDeadline = "2 / 2 / 2004" sql = "UPDATE TABLE1 SET EXPIRED = GETDATE() WHERE EMP_KEY = @KEY AND ITEM = 'Extend Deadline' AND EXPIRED IS ...

Accessing header on datagrid (template column)
How can i access the control a hyperlink on templatecolumn whic exists on header template?  i tried this on datagrid's item created method but doesnt work Dim lbl As HyperLink = DirectCast(dgI.FindControl("HyperLink7"), HyperLink) lbl.Visible = False   how to access the header template and a hyperlink which exists there ? Use RowCreated eventtry something like that were  templeteColIndex = index of you templete column    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    {       ...

accessing data in a datagrid template column
Can anyone out there tell me how to access data that is placed in a datagrid template column? I am coding in From what part of your code do you want to access it? If you want to access it while the grid is being databound, you can use ItemDataBound. Elsehwere, you'll need to iterate the items in the grid. Either way, you get to the specific column via the DataListItem.FindControl method. When you set up the template column, you put controls in the column and give them IDs. FindControl will return the control in the column, based on its id. You then get the data from the control.Ji...

Accessing Data in DataGrid Template Column
Hi: I have a Datagrid (called dgDataGrid) with a bound column that display a textbox when the user clicks the Edit button on a row of the datagrid. I can access the cell (cell [0]) of the clicked row and save its content (in OldValue) before the user enters a new entry with the following C# code: private void dgDataGridEdit_Click(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e){      string OldValue = dgDataGrid.SelectedItem.Cells[0].Text.ToString();      dgDataGrid.EditItemIndex = (int)e.Item.ItemIndex; &...

getting access to gridview data to set a template column
Hello.  I have a gridview which contains a checkbox item template. I need to set this initially to checked if another bound boolean data item in the grid is true (i.e. ticked in the gridview). Similarly, I have a dropdownlist whose selectedvalue I need to select to match a bound string data item in the grid. How can I do this? FindControl seems to be a common method of access the data, but the bool and string items in the grid do not have an ID.  So far, I've just managed to set the checkbox item template to true in the gridview RowCreated event as follows, but need to ...

row data of the grid if the template column checkbox control is checked
i have a datagrid and template columns with checkboxs for that particular column. I need to get the row data of the grid if the column checkbox is checked. how do i do that.? you need to loop through all the rows and get the checked one.. hth***********************Dinakar NethiLife is short. Enjoy it.***********************...

How to access datagrid template column textbox text in
I just want to access text from datagrid template column textbox,any one who knows solutions or suggestions(through codes), pls share it.Thanks in advance.  to access text from template column textbox... use FindControl() method... for Ex.,Label1.Text = ((TextBox)GridView1.Rows[0].Cell[0].FindControl("txtTextBox")).Text; //assuming that First Column; cells[0] is the TextBox Column... the code will retrieve text from first column textbox of the first row and assign to can refer to this link also.. Control.FindControl Method (String) (System.Web.UI)  Edit...

Cannot get index of datagrid row when I click on an image button in template column
I have the following code. I'm trying to get the method moveUp to fire off when a little up arrow is clicked. It works fine as a buttoncolumn because then I can use e.Item.ItemIndex to get the selected row index but how do I get an imagebutton within a template column to do this- it always comes back null at the moment. Btw, my datagrid is bound to an array list so I need the index of the row. Thanks in advance.  1 <%@ Page Language="vb" %> 2 <%@ Import Namespace="System.Web.Mail" %> 3 <%@ Import Namespace="System.Data" %...

DataGrid: Bound Column vs Template Column
I have a datagrid with a bound column and a template column as given below.When I scan through the grid, I get the value of the first column (bound column). However, I cannot get the value of the second column (Template Column). I can see the data in both columns though.////////////////////////////////////////////////////////////////////////////////////<AlternatingItemStyle BackColor="#e0e0e0"></AlternatingItemStyle><Columns><asp:BoundColumn DataField="Facility" HeaderText="Facility"> </asp:BoundColumn><asp:TemplateColumn HeaderText="Facility2"><ItemSty...

Solution Title: Datagrid Template Column Radio Button (get access to the rb control)
Hi. I have a radio button in a template column of a datagrid. All I need to know is how to get access to this to be able to set the radio button as checked or unchecked (depending on the results of a query). I'm trying this: Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("rbactionneeded"), RadioButton) If ansval = "Y" Then 'result from stored procedure rbactionneeded.Checked = True End if (very simplified). I'm getting an erro...

How to get Rows and Columns collection in a data bound server control, independent of DataSource attached.
Hi,   In my custom data bound server control (Inherited from existing server control). I need to get the column collection of the data source. I had tried to get the collection by   base.GetData()             This returns DataSourceView, which I can not enumerate to get rows and columns collection. I want to use it as DataTable/ DataView.   and base.GetDataSource()             This return IdataSource. I am not get...

Datagrid: Add a Main header row, above the column headers
Is this possible?  How is this done? -smc~ Remember To Mark The Posts Which Helped You As The ANSWER ~ Yes...Handle the ItemCreated event.  I have a sample using the GridView that can give you the idea,Phillip Williams, Do you have any examples using DataGrid in VB? Can the Header be displayed above the column headers?  -smc~ Remember To Mark The Posts Which Helped You As The ANSWER ~,Phillip Williams,http:...

getting the underlying for a datagrid row when the data source is a data reader
HiWhen the data source for my datagrid is a dataset I get the corresponding DataRow for the selected DataGridItem as follows:Dim pk as integer = grid.DataKeys(grid.selectedIndex)Dim dt as DataTable = grid.DataSource.Tables(grid.DataMember)Dim dr as DataRow = dt.Rows.Find(pk)How do I do the equivalent when the data source is a data reader. Thanks andrea...

Web resources about - How to access a CheckBox in the header row of a Template Column in a data-bound DataGrid -

Aging out of bounds: New demographic data show how diversely different species age and biologists cannot ...
Despite aging being one the hottest topic in the media recently, scientists have no coherent explanation for it. New demographic data on humans, ...

Resources last updated: 12/26/2015 12:00:54 AM