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

[Solved] Dynamic controls inside Grid

6 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ConsumentenClaim
Top achievements
Rank 1
ConsumentenClaim asked on 29 Mar 2013, 08:11 AM
Hi,

I have seen many topics about how to achieve this. However I am still struggling to get this done. I read that you have to create and add your controls in the ItemCreated event, and bind to it's data inside ItemDataBound. However, this event is never fired on postback, and the controls disappear.

Let's say I try to add a RadButton like below.

How can I keep it working on postback?

protected void grid_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem)
    {
        if (e.Item.DataItem != null)
        {
            int contractid = (e.Item.DataItem as Contract).ContractId;
            var records = GetRecords(contractid);
 
            foreach (var record in records)
            {
                var button = new RadButton();
                button.Text = contractid.ToString();
                button.Click += delegate
                {
                    button.Text = "fired!";
                };
 
                var phContract = (PlaceHolder)e.Item.FindControl("placeholder");
                phContract.Controls.Add(contract);
            }
        }
    }
}

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Apr 2013, 09:59 AM
Hi,

In general the proper place for adding controls to the grid items is in ItemCreated. But in the case of adding controls to the cells of GridColumn,you cannot use ItemCreated only, but a combination of ItemCreated and ItemDataBound. This is due to the fact that the control created in ItemCreated will be erased when data-binding this control. Also, if you create the control in ItemDataBound when the controls are created from ViewState, the grid will not raise ItemDataBound, and the control will not be created and would not raise postback events. The solution for such cases is to create the control in ItemDataBound and recreate this control if needed on ItemCreated for subsequent postbacks.
C#:
protected void grid_ItemCreated(object sender, GridItemEventArgs e)
{
  . . .
 foreach (var record in records)
  {
                var button = new RadButton();
                button.Text = contractid.ToString();
                button.Click += delegate
                {
                    button.Text = "fired!";
                };
  
                var phContract = (PlaceHolder)e.Item.FindControl("placeholder");
                phContract.Controls.Add(contract);
            }
}
protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
  . . .
 foreach (var record in records)
  {
                var button = new RadButton();
                button.Text = contractid.ToString();
                button.Click += delegate
                {
                    button.Text = "fired!";
                };
  
                var phContract = (PlaceHolder)e.Item.FindControl("placeholder");
                phContract.Controls.Add(contract);
            }
        }

Thanks,
Shinu.
0
ConsumentenClaim
Top achievements
Rank 1
answered on 01 Apr 2013, 08:23 PM
I see,

However, if you look at line 7 in my code:
int contractid = (e.Item.DataItem as Contract).ContractId;

How can I reference this DataItem inside ItemDataBound? Because in ItemDataBound e.Item.DataItem would be null.
0
Shinu
Top achievements
Rank 2
answered on 02 Apr 2013, 07:20 AM
Hi,

I am not quite sure about your requirement. One suggestion is to access it as datakeyvalue as shown below.
C#:
void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string contractid = item.GetDataKeyValue("Id").ToString();
        }
}

Thanks,
Shinu.
0
ConsumentenClaim
Top achievements
Rank 1
answered on 02 Apr 2013, 08:00 AM
Hello Shinu,

Thanks for your reply.

The following exception is raised when I try to call item.GetDataKeyValue():

   - Object reference not set to an instance of an object

item seem to be null (see screenshot).
0
Accepted
Shinu
Top achievements
Rank 2
answered on 03 Apr 2013, 04:34 AM
Hi,

Try setting the following.
aspx/:
<MasterTableView DataKeyNames="Id" >

Thanks,
Shinu.
0
ConsumentenClaim
Top achievements
Rank 1
answered on 04 Apr 2013, 04:16 PM
Works like a charm!
Tags
Grid
Asked by
ConsumentenClaim
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
ConsumentenClaim
Top achievements
Rank 1
Share this question
or