I have two radgrids on a page and export is not working on the first grid. In fact, when you click the export button, that grid's header command and filter rows disappear. Sorting, filtering, and paging work just fine, but not exporting. Here's my code:
XML Input File:
ASPX Code:
.cs Code:
XML Input File:
<?xml version="1.0" encoding="utf-8" ?> |
<years> |
<year value="2010" name="rat"></year> |
<year value="2011" name="cat"></year> |
<year value="2012" name="moose"></year> |
<year value="2013" name="deer"></year> |
<year value="2014" name="tiger"></year> |
<year value="2015" name="dragon"></year> |
<year value="2016" name="skunk"></year> |
<year value="2017" name="armadillo"></year> |
<year value="2018" name="dog"></year> |
<year value="2019" name="goldfish"></year> |
<year value="2020" name="koi"></year> |
<year value="2021" name="catfish"></year> |
<year value="2022" name="snake"></year> |
<year value="2023" name="beaver"></year> |
<year value="2024" name="dalmation"></year> |
<year value="2025" name="bluebird"></year> |
<year value="2026" name="boar"></year> |
<year value="2027" name="toad"></year> |
<year value="2028" name="mouse"></year> |
<year value="2029" name="salamander"></year> |
<year value="2030" name="alligator"></year> |
</years> |
ASPX Code:
<form id="form1" runat="server"> |
<div> |
<asp:ScriptManager runat="server" ID="ScriptManager1"> |
</asp:ScriptManager> |
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> |
<AjaxSettings> |
<telerik:AjaxSetting AjaxControlID="rgGrid"> |
<UpdatedControls> |
<telerik:AjaxUpdatedControl ControlID="rgGrid" /> |
</UpdatedControls> |
</telerik:AjaxSetting> |
</AjaxSettings> |
</telerik:RadAjaxManager> |
<table> |
<tr> |
<td> |
<telerik:RadGrid ID="rgGrid" runat="server" OnNeedDataSource="rgGrid_NeedDataSource" Width="45%" |
OnItemDataBound="rgGrid_ItemDataBound" OnItemCreated="rgGrid_ItemCreated" AutoGenerateColumns="true"> |
<MasterTableView> |
<CommandItemTemplate> |
<div style="padding: 5px;" align="right"> |
<strong>Export: </strong><asp:ImageButton ID="ibExportExcel" runat="server" |
ImageUrl="~/images/editorIcons/MSExcell_32.png" ToolTip="Export to Excel" OnClick="ibExportExcel_Click" /> |
<asp:ImageButton ID="ibExportWord" runat="server" ImageUrl="~/images/editorIcons/MSWord_32.png" ToolTip="Export to Word" |
OnClick="ibExportWord_Click" /></div> |
</CommandItemTemplate> |
</MasterTableView> |
</telerik:RadGrid> |
</td><td><telerik:RadGrid ID="rgGrid2" runat="server" OnNeedDataSource="rgGrid2_NeedDataSource" Width="45%" |
OnItemDataBound="rgGrid2_ItemDataBound" OnItemCreated="rgGrid2_ItemCreated" AutoGenerateColumns="true"> |
<MasterTableView> |
<CommandItemTemplate> |
<div style="padding: 5px;" align="right"> |
<strong>Export: </strong><asp:ImageButton ID="ibExportExcel2" runat="server" |
ImageUrl="~/images/editorIcons/MSExcell_32.png" ToolTip="Export to Excel" OnClick="ibExportExcel2_Click" /> |
<asp:ImageButton ID="ibExportWord2" runat="server" ImageUrl="~/images/editorIcons/MSWord_32.png" ToolTip="Export to Word" |
OnClick="ibExportWord2_Click" /></div> |
</CommandItemTemplate> |
</MasterTableView> |
</telerik:RadGrid> |
</td></tr></table> |
</div> |
</form> |
.cs Code:
public partial class WebAdmin_TestGrid_Default : System.Web.UI.Page |
{ |
DataSet ds = new DataSet(); |
bool isForExport = false; |
bool isForExport2 = false; |
protected void Page_Load(object sender, EventArgs e) |
{ |
rgGrid.PageSize = Profile.PreferredGridLines; |
rgGrid2.PageSize = Profile.PreferredGridLines; |
} |
#region Grid Logic |
protected void rgGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
{ |
ds = new DataSet(); |
string myXMLFile = Server.MapPath("~/App_Data/YearsTest.xml"); |
System.IO.FileStream fsReadXML = new System.IO.FileStream(myXMLFile, System.IO.FileMode.Open); |
try |
{ |
ds.ReadXml(fsReadXML); |
} |
catch (Exception ex) |
{ |
Master.SetMessage(ex.ToString()); |
} |
finally |
{ |
fsReadXML.Close(); |
} |
rgGrid.DataSource = ds; |
} |
protected void rgGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) |
{ |
// Hide filter row in export. |
if (e.Item is GridFilteringItem && isForExport) |
e.Item.Visible = false; |
// Pull Number of Entries per page in page from web.config appsettings. |
if (e.Item is GridPagerItem) |
{ |
GridPagerItem item = e.Item as GridPagerItem; |
RadComboBox combo = item.FindControl("PageSizeComboBox") as RadComboBox; |
combo.AutoPostBack = true; |
combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged); |
} |
} |
protected void rgGrid_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridPagerItem) |
{ |
GridPagerItem item = e.Item as GridPagerItem; |
RadComboBox combo = item.FindControl("PageSizeComboBox") as RadComboBox; |
combo.Items.Clear(); |
string strPageSizes = ConfigurationManager.AppSettings["PageSizes"]; |
string[] arrPageSizes = strPageSizes.Split(','); |
for (int x = 0; x < arrPageSizes.Length; x++) |
{ |
combo.Items.Add(new RadComboBoxItem(arrPageSizes[x], arrPageSizes[x])); |
} |
combo.SelectedValue = rgGrid.MasterTableView.PageSize.ToString(); |
} |
} |
void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) |
{ |
rgGrid.PageSize = int.Parse((o as RadComboBox).SelectedValue); |
rgGrid.Rebind(); |
} |
protected void ibExportExcel_Click(object sender, ImageClickEventArgs e) |
{ |
isForExport = true; |
rgGrid.MasterTableView.ExportToExcel(); |
} |
protected void ibExportWord_Click(object sender, ImageClickEventArgs e) |
{ |
isForExport = true; |
rgGrid.MasterTableView.ExportToWord(); |
} |
#endregion |
#region Grid2 Logic |
protected void rgGrid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
{ |
ds = new DataSet(); |
string myXMLFile = Server.MapPath("~/App_Data/YearsTest.xml"); |
System.IO.FileStream fsReadXML = new System.IO.FileStream(myXMLFile, System.IO.FileMode.Open); |
try |
{ |
ds.ReadXml(fsReadXML); |
} |
catch (Exception ex) |
{ |
Master.SetMessage(ex.ToString()); |
} |
finally |
{ |
fsReadXML.Close(); |
} |
rgGrid2.DataSource = ds; |
} |
protected void rgGrid2_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) |
{ |
// Hide filter row in export. |
if (e.Item is GridFilteringItem && isForExport) |
e.Item.Visible = false; |
// Pull Number of Entries per page in page from web.config appsettings. |
if (e.Item is GridPagerItem) |
{ |
GridPagerItem item = e.Item as GridPagerItem; |
RadComboBox combo = item.FindControl("PageSizeComboBox") as RadComboBox; |
combo.AutoPostBack = true; |
combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged2); |
} |
} |
protected void rgGrid2_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridPagerItem) |
{ |
GridPagerItem item = e.Item as GridPagerItem; |
RadComboBox combo = item.FindControl("PageSizeComboBox") as RadComboBox; |
combo.Items.Clear(); |
string strPageSizes = ConfigurationManager.AppSettings["PageSizes"]; |
string[] arrPageSizes = strPageSizes.Split(','); |
for (int x = 0; x < arrPageSizes.Length; x++) |
{ |
combo.Items.Add(new RadComboBoxItem(arrPageSizes[x], arrPageSizes[x])); |
} |
combo.SelectedValue = rgGrid2.MasterTableView.PageSize.ToString(); |
} |
} |
void combo_SelectedIndexChanged2(object o, RadComboBoxSelectedIndexChangedEventArgs e) |
{ |
rgGrid2.PageSize = int.Parse((o as RadComboBox).SelectedValue); |
rgGrid2.Rebind(); |
} |
protected void ibExportExcel2_Click(object sender, ImageClickEventArgs e) |
{ |
isForExport2 = true; |
rgGrid2.MasterTableView.ExportToExcel(); |
} |
protected void ibExportWord2_Click(object sender, ImageClickEventArgs e) |
{ |
isForExport2 = true; |
rgGrid2.MasterTableView.ExportToWord(); |
} |
#endregion |
} |