Hi Sandro,
I have tried your code and it is working fine.
But in my case, it is not working. Here i have pasted my sample code. Please have a look at it and let me know if anything wrong in my code.
Design Code
<%@ Register Src="~/CMSAdminControls/UI/UniGrid/UniGrid.ascx" TagName="UniGrid" TagPrefix="cms" %>
<%@ Register Namespace="CMS.UIControls.UniGridConfig" TagPrefix="ug" Assembly="CMS.UIControls" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Grid.ascx.cs" Inherits="CMSWebParts_Feedback_Grid" %>
<br />
<br />
<asp:Button ID="btn" runat="server" Text="Upload" OnClick="btn_Click" />
<asp:LinkButton ID="lnkExportAll" runat="server" OnClick="btnExportall_Click">
<asp:Label ID="lblExportAll" runat="server" Text="Export All"></asp:Label>
</asp:LinkButton>
<br />
<br />
<br />
<br />
<cms:UniGrid ID="customGrid" runat="server" ObjectType="cms.user" Columns="UserID, UserName" OrderBy="UserName">
<GridActions>
<ug:Action Name="edit" Caption="$General.Edit$" Icon="Edit.png" />
<ug:Action Name="export" Caption="Export" Icon="Export.png" />
</GridActions>
<GridColumns>
<ug:Column Source="UserName" Caption="$general.username$" Width="100%" />
</GridColumns>
</cms:UniGrid>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.SiteProvider;
using CMS.GlobalHelper;
using CMS.CMSHelper;
using CMS.SettingsProvider;
using System.Data;
using CMS.FormControls;
using CMS.Ecommerce;
using CMS.PortalControls;
using CMS.UIControls;
using CMS.DataEngine;
using CMS.EventLog;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;
public partial class CMSWebParts_Feedback_Grid : CMS.PortalControls.CMSAbstractWebPart
{
protected void Page_Load(object sender, EventArgs e)
{
customGrid.OnAction += customGrid_OnAction;
}
protected void customGrid_OnAction(string actionName, object actionArgument)
{
if (actionName == "export")
{
try
{
GeneralConnection cn1 = ConnectionHelper.GetConnection();
DataSet ds = null;
QueryDataParameters parameters1 = new QueryDataParameters();
ds = cn1.ExecuteQuery("CMS.User.selectall", null);
HttpResponse response = HttpContext.Current.Response;
string filename = "SampleTest.xls";
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// create a string writer
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
GridView dg = new GridView();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
catch (Exception ex)
{
}
}
}
protected void btnExportall_Click(object sender, EventArgs e)
{
try
{
GeneralConnection cn1 = ConnectionHelper.GetConnection();
DataSet ds = null;
QueryDataParameters parameters1 = new QueryDataParameters();
ds = cn1.ExecuteQuery("CMS.User.selectall", null);
HttpResponse response = HttpContext.Current.Response;
string filename = "SampleTest.xls";
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// create a string writer
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
GridView dg = new GridView();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
catch (Exception ex)
{
}
}
protected void btn_Click(object sender, EventArgs e)
{
// No code is written.
}
}
To generate the error, Follow the following steps :
1. First click on export from grid action buttons. ( o/p : Excel is generated with Data.)
2. Now click on Upload button. (error o/p : Excel is generated with data even there is no any code written in Upload button event.)
Thanks,
Atul patel