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

Postback "resets" cell controls

6 Answers 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sean Severson
Top achievements
Rank 1
Sean Severson asked on 02 Dec 2009, 04:34 PM
I'm adding a expand/collapse button for grouping in my grid.  I add this button on the ItemDataBound event and add the text (in this case it is a specialty) after the button by adding a literal control to the cell's controls collection.  On a postback the button and literal control that I added in the ItemDataBound event are not shown and only the text is displayed.

I tried adding code to the itemCreated event, but the DataItem property of the GridItem is nothing so I can't get to the specialty name in order to use it in the literal that I am adding as explained above.

How can I prevent my controls from disappearing from the grid cells?

Sean M. Severson

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 03 Dec 2009, 05:38 AM
Hi Sean M. Severson,

Checkout the code library which shows how to add checkbox in the group header item. You can try the same approach in order to add the button/literal controls to group header by adding in ItemCreated and ItemDataBound event handlers.
Selecting all items in a group with a checkbox in the group header item

Hope this helps,
Shinu.
0
Sean Severson
Top achievements
Rank 1
answered on 03 Dec 2009, 06:42 PM

Shinu,

 

The text values that will be displayed in each cell next to the expand/collapse button won't be hardcoded, but will come from a supplied datasource.  This is what I meant by the Dataitem property being nothing on the ItemCreated event.  If I do not add the expand/collapse button and the literal control, the text displays just find after a post back.  Where is it getting this information and how can I use it in the itemCreated event on a postback?

Sean M. Severson

0
Princy
Top achievements
Rank 1
answered on 04 Dec 2009, 09:21 AM
Hi,

The ItemDataBound event will not fire on a postback but only during the initial bind or during the rebind of the grid .The ItemCreated event instead fires on every postback.Thus it makes sense to add the dynamic controls to the collection here. Since the text of the dynamic control is  based on the datasource try adding  it to  a  ViewState in the ItemDataBound event and access it in the ItemCreated event.

C#

 

ArrayList arr = new ArrayList();

 

 

int count = 0;

 

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
        {  
            if (e.Item is GridGroupHeaderItem)  
            {  
 
                GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;  
                DataRowView groupDataRow = (DataRowView)e.Item.DataItem;  
                CheckBox check = new CheckBox();  
                check.AutoPostBack = true;  
                check.ID = "CheckAll";  
                check.Text = groupDataRow[0].ToString();  
                arr.Add(groupDataRow[0].ToString());  
                ViewState["group"] = arr;  
                item.DataCell.Controls.Add(check);  
            }  
       }  
 
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
{  
     
    if (e.Item is GridGroupHeaderItem)  
    {  
        GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;  
        CheckBox check = new CheckBox();  
        check.AutoPostBack = true;  
        check.ID = "CheckAll";  
        if (ViewState["group"] != null)   
        {  
            ArrayList arr =(ArrayList)ViewState["group"];  
            if(count<=arr.Count - 1) 
               check.Text = arr[count].ToString();  
            count++;  
        }  
        item.DataCell.Controls.Add(check);  
          
    }    

You can try out this and alter the logic to fit your requirement.

Thanks,
Princy
0
Sean Severson
Top achievements
Rank 1
answered on 04 Dec 2009, 04:21 PM
Princy,

I followed your suggestion and it worked great!  Thanks for the help.

Sean M. Severson
0
arnaud
Top achievements
Rank 1
answered on 10 Oct 2011, 04:06 PM
Hi,

Old post but still a pain two years later.

I think you should rework the way GridGroupHeaderItem works.
Today, just to customize the GridGroupHeaderItem with custom controls you have to :
- Use the itemcreated event
- Use the itemdataboundevent
- Use the viewstate to persist the value

Not really staightforward ...

Anyway this workaround works and it seems to be the only solution right now but it is really dirty !
0
Iana Tsolova
Telerik team
answered on 11 Oct 2011, 05:06 AM
Hi Arnaud,

I am glad to inform you that with the Q3 2011 release we are introducing GridGroupHeaderTemplate and GridGroupFooterTemplate. So you will be able to define directly in the mark what is to be displayed in the grid group header and footer.

Greetings,
Iana Tsolova
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
Sean Severson
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Sean Severson
Top achievements
Rank 1
Princy
Top achievements
Rank 1
arnaud
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or