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?
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;