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

FindControl not finding control in radgrid!

7 Answers 1057 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave Ranck
Top achievements
Rank 1
Dave Ranck asked on 02 Dec 2008, 10:38 PM
I have a radgrid with auto generated columns. I programmatically add a hidden field to one of the columns in the grid. When a certain event fires, I call a method to loop through each of the rows in the grid, then each of the columns to find the hidden field. I've tried a couple of different approaches but I cannot seem to retrieve the hidden field.

void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.Item ||
            e.Item.ItemType == GridItemType.AlternatingItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            HiddenField hiddenField = new HiddenField();
            hiddenField.Value = "HiddenFieldValue";
            hiddenField.ID = "HiddenFieldId";
            item[ColumnName].Controls.Add(hiddenField);
        }
    }

private void GetHiddenFieldValue()
{
        foreach (GridDataItem item in this.RadGrid.MasterTableView.Items)
        {
            foreach (GridColumn column in this.RadGrid.MasterTableView.AutoGeneratedColumns)
            {
                    HiddenField hiddenField1 = (HiddenField)item[column.UniqueName].FindControl("HiddenFieldId");
                    HiddenField hiddenField2 = (HiddenField)item[column].FindControl("HiddenFieldId");       

                    // more code here
            }           
        }
 }


Thanks for your help!

7 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 2
answered on 03 Dec 2008, 01:51 AM
I would view source, and try to see the hidden fields?
0
Shinu
Top achievements
Rank 2
answered on 03 Dec 2008, 05:32 AM
Hi Mike,

Try adding the hidden field in the ItemCreated event instead of ItemDataBound event and see whether it is helpful.

Shinu
0
Dave Ranck
Top achievements
Rank 1
answered on 03 Dec 2008, 01:36 PM
Thanks for your reply.
The hidden fields are there, but their id's are changed from "HiddenFieldId" to "ctl00_cphContent_ctl00_Control_RadGrid_ctl01_ctl04_HiddenFieldId" - could this be the problem?


<input type="hidden" name="ctl00$cphContent$ctl00$Control$RadGrid$ctl01$ctl04$HiddenFieldId" id="ctl00_cphContent_ctl00_Control_RadGrid_ctl01_ctl04_HiddenFieldId" value="377614f6-335f-4d0e-861d-0ef33b93e751" />
0
Dave Ranck
Top achievements
Rank 1
answered on 03 Dec 2008, 01:37 PM
Shinu - unfortunately that didn't work. When I view source the hidden field is not there. Thanks for your reply.
0
Dave Ranck
Top achievements
Rank 1
answered on 03 Dec 2008, 10:24 PM
For anyone that's interested - my issue was being caused because I was not recreating the hidden fields in the grid after the postback.
0
Phil
Top achievements
Rank 2
answered on 13 Dec 2008, 03:47 AM
I use a lot of hidden fields, but I code them into the grid.  I will use the Telerik template control:

<telerik:GridTemplateColumn HeaderText="Template">
    <ItemTemplate>
        Item
    </ItemTemplate>
    <EditItemTemplate>
        Edit
    </EditItemTemplate>
</telerik:GridTemplateColumn>

Replace Item with a Label, checkbox, etc.  And replace Edit with a dropdown, combobox textbox, etc and the hidden bound field with the row.  Such as the following I am keeping the Id while displaying only the name:

<asp:HiddenField id="VendorIdHidden" runat="server" Value='<%# Bind("VendorId") %>' />
<asp:Label id="VendorNameLabel" runat="server" Text='<%# Bind("VendorName") %>' />
0
Dimo
Telerik team
answered on 13 Dec 2008, 08:35 AM
Hi Mike,

You can add controls to RadGrid cells in the ItemCreated event handler. However, if you add controls to a bound column, they will be flushed by the datasource data during the ItemDataBound event. That's why it is better to use GridTemplateColumns for such purposes.

On the other hand, you can use the ItemDataBound event handler to add controls programmatically and they will not be flushed by the datasource data. But in this case you will not be able to access these controls in the ItemCommand event handler, because it is executed before ItemDataBound. So I would still use the ItemCreated event.

Here is some more information about the RadGrid event sequence:

http://www.telerik.com/help/aspnet-ajax/grdeventsequence.html

By the way, regarding your GetHiddenFieldValue method, it is not good to iterate through all items and for each item to iterate through all columns - you will execute some code a lot more times than necessary. After all, you know in which column the HiddenField controls are, so target this column only.

Here is a working example:


<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<script runat="server"
 
    protected void RadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) 
    { 
        DataTable dt = new DataTable(); 
        DataRow dr; 
        int rowsNum = 5
 
        dt.Columns.Add("Column1"); 
        dt.Columns.Add("Column2"); 
        dt.Columns.Add("Column3"); 
 
        for (int i = 1; i <= rowsNum; i++) 
        { 
            dr = dt.NewRow(); 
 
            dr["Column1"] = String.Format("Column1 Row{0}", i); 
            dr["Column2"] = String.Format("Column2 Row{0}", i); 
            dr["Column3"] = String.Format("Column3 Row{0}", i); 
             
            dt.Rows.Add(dr); 
        } 
 
        (sender as RadGrid).DataSource = dt
    } 
 
    protected void RadGrid_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (e.Item as GridDataItem); 
            HiddenField hiddenField = new HiddenField(); 
            hiddenField.Value = "1"
            hiddenField.ID = "HiddenFieldId"
            item["TemplateColumn"].Controls.Add(hiddenField); 
 
            LiteralControl literalControl = new LiteralControl(); 
            literalControl.ID = "literalControlId"
            literalControl.Text = hiddenField.Value; 
            item["TemplateColumn"].Controls.Add(literalControl); 
        } 
    } 
 
    protected void RadGrid_ItemCommand(object sender, GridCommandEventArgs e) 
    { 
        foreach (GridDataItem item in (sender as RadGrid).MasterTableView.Items) 
        { 
            (item["TemplateColumn"].FindControl("HiddenFieldId") as HiddenField).Value = "2"
            (item["TemplateColumn"].FindControl("literalControlId") as LiteralControl).Text = "2"
        } 
    } 
     
</script> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>RadControls for ASP.NET AJAX</title> 
</head> 
<body> 
<form id="form1" runat="server"
<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
 
<p>Click on a RadGrid row to change the values of all hidden fields in the template column from 1 to 2.</p> 
 
<telerik:RadGrid 
    ID="RadGrid1" 
    runat="server" 
    OnNeedDataSource="RadGrid_NeedDataSource" 
    OnItemCreated="RadGrid_ItemCreated" 
    OnItemCommand="RadGrid_ItemCommand"
    <ClientSettings EnablePostBackOnRowClick="true" /> 
    <MasterTableView> 
        <Columns> 
            <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="TemplateColumn" /> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 
 
</form> 
</body> 
</html> 
 


Regards,
Dimo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Dave Ranck
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Dave Ranck
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or