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

ItemDataBound can't find my controls created in ItemCreated

1 Answer 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tami
Top achievements
Rank 1
Tami asked on 12 Oct 2010, 05:39 PM
Hi,
I have a RadGrid with autogenerated columns.  As the rows are building, I need to add a label and a dropdownlist to one of the columns.  Everything works fine if I put everything in ItemDataBound and have my grid hierarchyloadmode=client (code snippet #1).  This is slow, so I want to use ServerBind.  In doing that, my controls disappear on postback.  I have found examples where I should create my control in ItemCreated and ItemDatabound, but this does not seem to work for me (code snippet #2).  I get an object reference error because it cannot find the control.  What am I doing wrong?

//code snippet #1 - this works when in hierarchyloadmode=Client
protected
void grdVerification_ItemDataBound(object sender, GridItemEventArgs e) 
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
  
                //build and set the value for the Escalators
                GridTableCell cellEscalatorID = (GridTableCell)item["EscalatorID"];
                GridTableCell cellEscalatorName = (GridTableCell)item["EscalatorName"];
                //add a label to store the EscalatorName
                Label lblEscalatorName = new Label();
                lblEscalatorName.ID = "lblEscalatorName";
                lblEscalatorName.Text = cellEscalatorName.Text;
                cellEscalatorName.Controls.Add(lblEscalatorName);
  
                DropDownList ddlEscalators = new DropDownList();
                ddlEscalators.DataSourceID = "dsrcEscalators";
                ddlEscalators.DataTextField = "EscalatorName";
                ddlEscalators.DataValueField = "EscalatorID";
                ddlEscalators.Attributes.Add("style", "display:none");
                ddlEscalators.Attributes.Add("OnChange", "ddlEscalators_Change(this)");
                ddlEscalators.ID = "ddlEscalators";
                cellEscalatorName.Controls.Add(ddlEscalators);
                (item.FindControl("ddlEscalators") as DropDownList).SelectedValue = cellEscalatorID.Text;
//code snippet #2 - this errors when it can't find the control.  As you can see, I tried calling it by referencing
// the cell's controls and the item's controls.  Neither approach works.
protected
void grdVerification_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
  
                //build and set the value for the Escalators
                GridTableCell cellEscalatorName = (GridTableCell)item["EscalatorName"];
                //add a label to store the EscalatorName
                Label lblEscalatorName = new Label();
                lblEscalatorName.ID = "lblEscalatorName";
                lblEscalatorName.BackColor = System.Drawing.Color.Orange;
                cellEscalatorName.Controls.Add(lblEscalatorName);
  
                DropDownList ddlEscalators = new DropDownList();
                ddlEscalators.DataSourceID = "dsrcEscalators";
                ddlEscalators.DataTextField = "EscalatorName";
                ddlEscalators.DataValueField = "EscalatorID";
                ddlEscalators.Attributes.Add("style", "display:none");
                ddlEscalators.Attributes.Add("OnChange", "ddlEscalators_Change(this)");
                ddlEscalators.ID = "ddlEscalators";
                cellEscalatorName.Controls.Add(ddlEscalators);
                  
            }
  
        }
         
        protected void grdVerification_ItemDataBound(object sender, GridItemEventArgs e) 
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
  
                ////build and set the value for the Escalators
                GridTableCell cellEscalatorID = (GridTableCell)item["EscalatorID"];
                GridTableCell cellEscalatorName = (GridTableCell)item["EscalatorName"];
                (cellEscalatorName.FindControl("lblEscalatorName") as Label).Text = cellEscalatorName.Text;
  
                (item.FindControl("ddlEscalators") as DropDownList).SelectedValue = cellEscalatorID.Text;

 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Oct 2010, 11:29 AM
Hello Tami,

In order to avoid this issue, create the control both in ItemCreated and ItemDataBound event.

C#:
protected void grdVerification_ItemCreated(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem item = (GridDataItem)e.Item;
               GridTableCell cellEscalatorName = (GridTableCell)item["EscalatorName"];
               Label lblEscalatorName = new Label();
               lblEscalatorName.ID = "lblEscalatorName";
               lblEscalatorName.BackColor = System.Drawing.Color.Orange;
               cellEscalatorName.Controls.Add(lblEscalatorName);
            }
        }
 
   protected void grdVerification_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           GridTableCell cellEscalatorName = (GridTableCell)item["EscalatorName"];
           Label lblEscalatorName = new Label();
           lblEscalatorName.ID = "lblEscalatorName";
           lblEscalatorName.BackColor = System.Drawing.Color.Orange;
           cellEscalatorName.Controls.Add(lblEscalatorName);
           (item.FindControl("lblEscalatorName") as Label).Text = cellEscalatorName.Text;
       }
   }

Thanks,
Princy.
Tags
Grid
Asked by
Tami
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or