GridView的导出内容到Excel表格中RUNAT =即使表单的Runat服务器错误=服务器 [英] Exporting Gridview content to excel form runat=server error even though form has runat=server
问题描述
好了,所以这里的情况。
Ok so here is the situation.
我的GridView内容被导出到Excel工作表。基本上我有一个按钮,调用方法(例如btnExcel_Clicked)。在C#code的背后,是这样
I have Gridview content being exported to an excel sheet. Basically I have a button that calls a method (e.g. btnExcel_Clicked). The c# code behind is as such
protected void btnExcel_Click ( object sender, EventArgs e )
{
Response.Clear( );
Response.AddHeader( "content-disposition", "attachment; filename=" + "test" + "_Registration_Forms.xls" );
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Page.EnableViewState = false;
System.IO.StringWriter stringWrite = new System.IO.StringWriter( );
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter( stringWrite );
clientGridView.RenderControl( htmlWrite ); //this is the name of my gridview
Response.Write( stringWrite.ToString( ) );
Response.End( );
}
当触发按钮,它就会显示错误:
When the button fires, it then shows the error:
System.Web.HttpException:类型GridView的控制clientGridView'必须放在与RUNAT =服务器的表单标签内
这表明以下code是哪里出现了错误:
Indicating that the following code is where the error occurred:
Line 217: clientGridView.RenderControl( htmlWrite );
为什么会变成这样,如果我的表确实有=服务器里面?
Why would this be if my form does indeed have the runat="server" inside it?
我怀疑这可能是由于我是如何使用的页面时,GridView被包裹在一个更新面板(不知道这会造成什么问题吗?),因此,我必须在页面顶部的一些javascript让页面刷新每个搜索查询。它看起来像这样:
I suspect it may be as a result of how I am using the page, the gridview is wrapped in an update panel (not sure if this would cause any problems?) and I have some javascript at the top of the page to allow the page to refresh each search query. Which looks like so:
<script type="text/javascript">
function RefreshUpdatePanel() {
__doPostBack('<%= txtClientName.ClientID %>', '');
$('#totals').fadeIn('slow');
$('#clientGrid').fadeIn('slow');
$('#btnReport').fadeIn('slow');
};
</script>
这基本上是我的触发更新面板,每次我输入一个值txtClientName
This basically just fires my update panel everytime I enter a value into txtClientName
我不知道如何任何这将是导致这样的错误。如果有人能提供一些线索在这个或指导我在正确的方向我将不胜感激。这是因为我的GridView控件放在更新面板内? arghhhh ..
I am not sure how any of this would be causing such an error. If someone could shed some light on this or guide me in the right direction I would be greatful. Is this because my gridview is placed within the update panel? arghhhh..
在此先感谢!
编辑:好的,第一个问题就解决了......但一个新的问题出现了。
ok first problem solved...but a new problem has arisen.
通过将该code在我的code背后:
by placing this code in my code behind:
public override void VerifyRenderingInServerForm ( System.Web.UI.Control control )
{
//confirms that an HtmlForm control is rendered for the
//specified ASP.NET server control at run time.
}
我可以让过去的错误,然而新的错误出现了:
I can get past that error, however a new error arose:
System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
呸。
编辑2:
好了问题大多得到解决。在@页部分正常进入EnableEventValidation =false时,此功能。然而,这是不安全的,现在是有办法解决此问题?
Edit 2: Ok problem mostly solved. By entering in EnableEventValidation ="false" in @ Page section, this functions properly. However, Is this now unsafe is there a way around this?
编辑3:
此外,有没有实现这一目标,而不在Microsoft Excel中失去了单元格边框的一种方式?
Edit 3: Also, is there a way of achieving this without losing the cell borders in microsoft excel?
推荐答案
我知道这并不直接雁渲染问题,但如果你的DataGrid绑定到数据集为什么不直接从数据集采用的方法导出,如在这个问题上接受雁:
I know this does not directly anser your rendering problem but if your datagrid is bound to a dataset why not export it directly from the dataset using a method such as the accepted anser in this question:
更新
如果你没有一个数据集,你可以很容易地生成一个使用的SqlDataAdapter和填充数据集,像这样:
If you do not have a dataset you can generate one easily using SQLDataAdapter and fill a dataset like so:
var dataAdapter = new SqlDataAdapter("Select * From table",
"Data Source=MySQLServerName;Initial Catalog=DatabaseName;Integrated Security=True");
var dataset = new DataSet();
dataAdapter.Fill(dataset);
更新2
如果您想从您的数据源使用SqlCommand文本,你可以这样做:
If you want to use the sqlcommand text from your datasource you can do this:
var dataAdapter = new SqlDataAdapter(mySqlDatasource.SelectCommand,
"Data Source=MySQLServerName;Initial Catalog=DatabaseName;Integrated Security=True");
这篇关于GridView的导出内容到Excel表格中RUNAT =即使表单的Runat服务器错误=服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!