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

NeedDataSource always called on past back

2 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ryan
Top achievements
Rank 1
ryan asked on 18 Jan 2012, 04:50 AM
I have a grid inside a user control.  The user control is recreated on postback.  After it is recreated the NeedDataSource always fires and the rebind reason is always "InitialLoad".  Also, none of my command handlers are called.  Am I not supposed to be recreating the control?

This is what I'm doing:

Creating the user control with the grid inside of it.
In the NeedDataSource, I bind it to the data (and I DON'T call DataBind or Rebind())
The grid shows up fine with all data in it.
Then when I press edit, it makes the fields editable.
Once I press update (which is next to "cancel"), it posts back, the user control (that contains the grid) is recreated by me in Page_Load, and NeedDataSource is called again.  The rebind reason is always "InitialLoad".

Notes:

No where am I turning off view state.
I'm using telerik version 2011.2.915.40.
The only place I'm bind to the grid is in NeedDataSource.

Any idea on what may be happening?  All I'm looking to do is having a grid that has items which I can edit.

Here is my grid definition:

<telerik:RadGrid ID="radGridEmails" runat="server" Skin="Transparent" AutoGenerateColumns="false">
                                  <MasterTableView EditMode="InPlace" >
                                           <Columns>
                                                                                <telerik:GridEditCommandColumn />
                                                                                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                                                                                    </telerik:GridBoundColumn>
                                                                                    <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email"  >
                                                                                    </telerik:GridBoundColumn>
                                                                                     
                                                                                </Columns>
                                                                            </MasterTableView>
                                                                             
                                                                        </telerik:RadGrid>

More markup in the user control:
<telerik:RadAjaxManagerProxy ID="RadAjaxManager2" runat="server"  >
            <AjaxSettings >
                <telerik:AjaxSetting AjaxControlID="radGridEmails"  >
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="radGridEmails"  />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
</telerik:RadAjaxManagerProxy>


Code in aspx Page_Load:

if (ViewState["currentlyLoadedSection"] != null)
                {
                    panelMain.Controls.Clear();
 
                    string ctrl = (string)ViewState["currentlyLoadedSection"];
                    UserControl uc = (UserControl) GetControlByType(ctrl);
                    uc.ID = ctrl;
 
                    panelMain.Controls.Add(uc);
                    Trace.Write("recreated section " + ctrl);
                }

Code in user control:

protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
 
            radGridEmails.UpdateCommand += new Telerik.Web.UI.GridCommandEventHandler(radGridEmails_UpdateCommand);
            radGridEmails.InsertCommand += new Telerik.Web.UI.GridCommandEventHandler(radGridEmails_InsertCommand);
            radGridEmails.DeleteCommand += new Telerik.Web.UI.GridCommandEventHandler(radGridEmails_DeleteCommand);
            radGridEmails.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(radGridEmails_NeedDataSource);
        }
 
protected void radGridEmails_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
 
                radGridEmails.DataSource = emailAddresses;
                Trace.Write(e.RebindReason.ToString());
 
        }

2 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 18 Jan 2012, 11:43 AM
Hello Ryan,

 Normally you do not need to recreate the control every time on PageLoad after postback. You can do this in Page_Init every time, or in Page_Load only the first time when the grid is not present on the page.
The rebind reason in your case is "InitialLoad" because every time the grid is recreated anew and not as a result of a postback. 
More information about programmatic creation can be found in this help topic:
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html 

Regards,
Marin
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
0
ryan
Top achievements
Rank 1
answered on 18 Jan 2012, 04:38 PM
This fixed the problem.  Thanks very much.
Tags
Grid
Asked by
ryan
Top achievements
Rank 1
Answers by
Marin
Telerik team
ryan
Top achievements
Rank 1
Share this question
or