This is a migrated thread and some comments may be shown as answers.

Difficulty accessing Controls in a GridDataItem

5 Answers 683 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 15 Jan 2012, 01:30 AM
I am building a DataTable dynamically at run time and I am also using templates in the Cells that again are dynamically loaded.
The template is currently extremely simple in that it consists solely a CheckBos and a HiddenField like so:
<asp:CheckBox ID="cb" runat="server" /> <asp:HiddenField ID="hf" runat="server" />

I trap the ItemDataBound Event of the Grid and have code similar to that below:
if (e.Item is GridDataItem)
{
    GridDataItem item = (GridDataItem)e.Item;
    DataRowView drv = (DataRowView)item.DataItem;
    for (int i = 0; i < rgTags.MasterTableView.Columns.Count; i++ )
    {
        string uniqueName = rgTags.MasterTableView.Columns[i].UniqueName;
        if (drv[i] is System.DBNull)
        {
            item[uniqueName].Controls[0].FindControl("cb").Visible = false;
        }
        else
        {
            AdminTag tag = (AdminTag)drv[i];
            ((CheckBox)item[uniqueName].Controls[0].FindControl("cb")).Text = tag.sText;
            ((HiddenField)item[uniqueName].Controls[0].FindControl("hf")).Value = tag.nTagId.ToString();
        }
    }
}

My question is why do I have to have the Controls[0]. before the FindControl() calls? I don't like hard coding things like that and would prefer a more elegant way to find the control in the template.

It is wo0rking now, but it's inelegant and it took me a long time to figure it out and I am sure it will get broken b y a future release.

Martin

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Jan 2012, 05:19 AM
Hello Martin,

In order to access the controls in TemplateColumn, you can use findControl method. Here is the sample code.
C#:
protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
   GridDataItem item = (GridDataItem)e.Item;
   CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
 }
}

Also check the following help documentation which explains more about accessing controls.
Accessing Cells and Rows.

-Shinu.
0
Martin
Top achievements
Rank 1
answered on 16 Jan 2012, 05:49 AM
I know it must get tiresome having to respond to all these requests, but yhou could do me the courtesy of reading my post before replying with an absurd comment.

Firstly, as I understand it, the GridDataItem refers to the ROW. The row has more than one column that has a CheckBox with an ID of "cb".
CheckBox cb = (CheckBox) item.FindControl("cb");

This would find the first such control. So I adjusted it to:

CheckBox cb = (CheckBox) item["UniqueName"].FindControl("cb");

But this didn't work = it returned null. So using the debugger I eventually discovered that:
CheckBox cb = (CheckBox)item["UniqueName"].Control[0].FindControl("cb");

did work. So I posted asking WHY the .Control[0] was necessary. Perhaps you would like to try again!.
0
Tsvetina
Telerik team
answered on 17 Jan 2012, 04:21 PM
Hi Martin,

Can you paste your ITemplate declaration and how you assign this template, so we can see how exactly you create and place the checkbox? In general, RadGrid would not allow you to have two checkboxes with the same ID inside one and the same GridDataItem. You would be getting:
Multiple controls with the same ID 'cb' were found. FindControl requires that controls have unique IDs.

This leads me to think that you have another naming container inside the GridDataItem but from the available code I cannot see one.

Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Martin
Top achievements
Rank 1
answered on 17 Jan 2012, 05:17 PM
Firstly, I'd like to apologize to Shinu for being a little rude in my earlier response. It was very late when I saw the post and I was tired and frustrated.

The columns are created dynamically as GridTemplateColumns - during creation item.ItemTemplate = LoadTemplate(filename);
The template is as shown in my initial post. The same template is used in all columns. This is why I did the FindControl, not on the item but on the item[uniquename] to restrict the search to the column in question. It appears that a TableCell is created, presumably as a NamingContainer. What the basic question is, is why does the following happen?

item[uniquename].FindControl("cb"); //returns null
 
item[uniquename].Controls[0].FindControl("cb"); // returns the correct CheckBox

It behaves as though the TableCell has its FindControl disabled.

I should also add that for a number of reasons I have changed the way I do this entire process so that I no longer use a RadGrid for the operation so the answer isn't important to me any more, apart from the intellectual curiosity as to how controls can get hidden from FindControl().
0
Tsvetina
Telerik team
answered on 19 Jan 2012, 12:25 PM
Hello Martin,

This should happen only if the control residing inside Controls[0] is a NamingContainer for the CheckBox. In the default case, it works to use FindControl on the cell, even on the whole row. 
As previously said, in a case, where Controls[0] does not contain a naming container, RadGrid would not allow you to have two checkboxes with the same ID in one GridDataItem, so FindControl() on the row would either find the single checkbox with this ID, or there is something wrong with the grid setup.
If you are still interested in an explanation, please post the server-side declaration of the ITemplate inheriting class used for the template columns.

All the best,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Martin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Martin
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or