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

Passing datatable to a web user control w/ radgrid

1 Answer 183 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve LaForge
Top achievements
Rank 1
Steve LaForge asked on 11 Jul 2012, 09:15 AM
I am trying to create a page that will display a dynamic set of results based on user input.  The gist is that the results will look something would contain specific info about a customer site, followed by a grid with the detailed results for that particular location.  Something like:

Site: sitename, city, state
    grid w/ satisfaction survey results

The user would be able to pick either a single site, all of the sites that reports to them, or to a subset.  Since this is a dynamic list, I thought it would be best to use a user control with the grid.  I already have a datatable that has the results for all of the selected sites, so I want to pass the datatable to the user control, then let the control apply a filter.

So I created a user control, and the code-behind for it looks like:

namespace SurveyResults
{
  public partial class resultsGrid : System.Web.UI.UserControl
  {
    private DataTable _dtResults = null;
 
    public DataTable dtResults
    {
      get
      {
        return _dtResults;
      }
      set
      {
        _dtResults = dtResults;
      }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        gridResults.DataSource = _dtResults;
        gridResults.Rebind();
    }
  }
}


Then I added a placeholder to my primary page, and added the following code:

// dtResults is a datatable that contains the results for all sites.  I have verified that it does contain data at this point
 
_resultsGrid = (resultsGrid)Page.LoadControl("resultsGrid.ascx");
_resultsGrid.dtResults = dtResults;
phResults.Controls.Add(_resultsGrid);

When I set a breakpoint on the set property for dtResults, I am only getting a null value.  So then when the page_load for the user control runs, the datasource is null.  I don't get an error, I just don't get a grid either.

I will be very much appreciative if someone can point out what it is that I am doing wrong here.

Thank you!

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 16 Jul 2012, 06:44 AM
Hello,

You could try to use the NeedDataSource event of RadGrid to bind the Grid in the user control declaration and remove the binding logic from the Page_Load event.

Another approach is to remove the binding logic from the user control and bind the grid inside the parent page. You could do this by using the user control reference with the FindControl method to get a reference to RadGrid. Then it is easy to hook the NeedDataSource event of RadGrid.

Give these suggestions a try and check whether the issue is resolved.

Greetings,
Andrey
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
Steve LaForge
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or