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

RadGrid Losing controls on postback

5 Answers 340 Views
Grid
This is a migrated thread and some comments may be shown as answers.
kranthi kiran pusala
Top achievements
Rank 1
kranthi kiran pusala asked on 20 Apr 2013, 10:34 AM
I have several RadGrids on a page which are reading data using a stored procedure.The autogeneratecolumns is set to true.
I am adding some controls to the rows dynamically on ItemDataBound. When I do a Sort/Filter on a RadGrid, that grid is rendering properly, but the other grids are losing the dynamically added controls.

I tried using the advanced databinding using the NeedDataSource and also doing a simple bind on every postback. It fails in both the scenarios.

   protected void myRadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
                   
     if (e.Item is GridDataItem)
                {
                    GridDataItem item = (GridDataItem)e.Item;
                    foreach (string hlc in hyperlinksColumns)
                    {
                        HyperLink link = new HyperLink();
                        Guid guid = new Guid();
                        link.ID = guid.ToString();
                        link.Text = item[hlc].Text; //accessing the GridBoundColumn to get the text
                        link.NavigateUrl = item[hlc + "_URL"].Text;
                        item[hlc].Controls.Add(link);
                    }
 
                    foreach (string ic in imageColumns)
                    {
                        Image image = new Image();
                        Guid guid = new Guid();
                        image.ID = guid.ToString();
                        image.ImageUrl = ImageLocation + item[ic + "_IMG"].Text;
                        item[ic + "_IMG"].Controls.Add(image);
                    }
 
 
                }
 
}

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Apr 2013, 02:07 PM
Hello,

When you do a sort/filter at that time needDataSource called automatically. When NeedDatasource event is executed at that time ItemDataBound bound event is executed, so it will generate dynamic control.

Now, when the page Postback at that time below method/code is not called that's why you are not able to see dynamic controls.So you have to called below method on every PostBack.

RadGrid1.Rebind();


Thanks,
Jayesh Goyani
0
kranthi kiran pusala
Top achievements
Rank 1
answered on 20 Apr 2013, 03:26 PM
Thanks Jayesh, But I get this error "Invalid postback or callback argument."  when I add "RadGrid1.Rebind();" to  the Page load.

Here is  exactly what I am doing.

I have written a SharePoint Visual Webpart Which contains a RadGrid. This RadGrid is populated using a stored procedure from code behind and the name of the stored procedure is read from webpart properties. I am using autogeneratecolumns = true. 
On Itemdatabound I am modifying some data and adding some controls, like hyperlink and image.( Code in my previous post)

Say I have a page and I added two webparts A and B, both same functionality, but using different stored procedures underneath.
Now, when I click on a header column for sorting or click on the paging control on the Webpart B Grid, Webpart A  Grid controls (hyperlinks and images) are lost. Grid in Webpart B is rendering fine. I guess this is because the Grid B is regenerated.

0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Apr 2013, 04:22 PM
Hello,

i don't know any thing about sharepoint.

But can you please check that if eventtarget is not a Radgrid then then called Rebind() in Page_prerender() event?

Thanks,
Jayesh Goyani
0
kranthi kiran pusala
Top achievements
Rank 1
answered on 22 Apr 2013, 02:21 PM
Yes. This workaround helps. Thank you. But, Rebinding every grid on postback is an overload. I am trying to find out why the controls are getting lost on postback. Am I missing something in the RadGrid Configuration? Please share your thoughts.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 22 Apr 2013, 04:40 PM
Hello,

As per .net framework functionality we have to add dynamically control on every time.
Please check below link.
Re-Creating Controls on Post-Back

1. You can also store your data in session/cache/viewstate.
2. You can also use grid's client side binding for same.

Am I missing something in the RadGrid Configuration? : No you are not missing anything.

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
kranthi kiran pusala
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
kranthi kiran pusala
Top achievements
Rank 1
Share this question
or