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

Export Grid Options in User Control

4 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 26 Aug 2008, 08:41 PM

Tell me if this is possible.

I have several webpages that have distinct RadGrids in them.  I would like to use the export options in a user control instead of having all the code below in each page that contains a Grid.

However, I don't know how to reference the RadGrid in the user controls code behind. 

In the User Control ascx

<asp:ImageButton ID="ImageButtonSave" ImageAlign="AbsMiddle" ImageUrl="~/images/13Save.gif" AlternateText="Save Grid Settings" OnClick="ButtonSave_Click" runat="server" /> 
<asp:ImageButton ID="ImageButtonExcel" OnClick="Button1_Click" ImageUrl="~/images/office/xls.gif" AlternateText="Export to Excel" runat="server" />    
<asp:ImageButton ID="ImageButtonWord" OnClick="Button2_Click" ImageUrl="~/images/office/doc.gif" AlternateText="Export to Word" runat="server" />   
<asp:ImageButton ID="ImageButtonPDF" OnClick="Button4_Click" ImageUrl="~/images/office/pdf.gif" AlternateText="Export to PDF" runat="server" />      

In the User Control ascx.cs however, I cannot reference the RadGrid in the aspx page.
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        ConfigureExport();  
        RadGrid1.MasterTableView.ExportToExcel();  
    }  
 
    protected void Button2_Click(object sender, System.EventArgs e)  
    {  
        ConfigureExport();  
        RadGrid1.MasterTableView.ExportToWord();  
    }  
 
    protected void Button3_Click(object sender, System.EventArgs e)  
    {  
        ConfigureExport();  
        RadGrid1.MasterTableView.ExportToCSV();  
    }  
 
    protected void Button4_Click(object sender, System.EventArgs e)  
    {  
        ConfigureExport();  
        RadGrid1.ExportSettings.Pdf.PageHeight = Unit.Parse("200mm");  
        RadGrid1.ExportSettings.Pdf.PageWidth = Unit.Parse("500mm");         
        RadGrid1.MasterTableView.ExportToPdf();  
    }  
 
    public void ConfigureExport()  
    {  
        RadGrid1.ExportSettings.ExportOnlyData = true;  
        RadGrid1.ExportSettings.IgnorePaging = true;  
        RadGrid1.ExportSettings.OpenInNewWindow = true;  
    }  
 

Your help would be appreciated, thank you!

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Aug 2008, 05:49 AM
Hi George.

Try accessing the page first. Then find the Grid in the page and export the Grid.

Here is the code to find the Grid in a Page:
protected void Button1_Click(object sender, System.EventArgs e)   
    {   
         
     RadGrid Grid1= (RadGrid)Page.FindControl("RadGrid1");  
     Grid1.MasterTableView.ExportToExcel();  
 
    }  

Here is code to find the Grid if it is in a Master page:
protected void Button1_Click(object sender, System.EventArgs e)   
    {   
 
    RadGrid  Grid1=  = this.Page.Master.FindControl("ContentPlaceHolder").FindControl("Grid1"); 
    Grid1.MasterTableView.ExportToExcel();  
  
   
    }  


Thanks
Princy.
0
Mike
Top achievements
Rank 1
answered on 28 Aug 2008, 12:54 PM
Princy,

Thanks for the response, I thought the first one was going to be the answer.  but it also threw an error.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 28:         ConfigureExport();
Line 29:         RadGrid Grid1 = (RadGrid)Page.FindControl("RadGrid1");
Line 30: Grid1.MasterTableView.ExportToExcel();Line 31:     }
Line 32: 

I am using a master page and a content place holders but when I use that code this is the other error.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'Telerik.Web.UI.RadGrid'. An explicit conversion exists (are you missing a cast?)

Source Error:

Line 27:     {
Line 28:         ConfigureExport();
Line 29: RadGrid Grid1= this.Page.Master.FindControl("ContentPlaceHolder").FindControl("RadGrid1"); Line 30:         Grid1.MasterTableView.ExportToExcel();
Line 31:     }

This is troubling me.
0
Richard van Diggelen
Top achievements
Rank 1
answered on 02 Sep 2008, 05:36 AM
You have an implicit conversion.

Try changing

RadGrid Grid1this.Page.Master.FindControl("ContentPlaceHolder").FindControl("RadGrid1"); 
with
RadGrid Grid1= (RadGrid)this.Page.Master.FindControl("ContentPlaceHolder").FindControl("RadGrid1"); 
0
Mike
Top achievements
Rank 1
answered on 02 Sep 2008, 12:15 PM
That works, but it opens in a the excel in the browers and does not prompt me to open or save in Excel.
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Richard van Diggelen
Top achievements
Rank 1
Share this question
or