DataGrid本来就是runat = server的

解决方案 »

  1.   

    因为你是读取的前端HTML代码来写入到excel的,在你的DataGrid控件中是否有按钮列或模板列之类的列?估计应该是导出时你把按钮列或模板列也进行导出所引起的错误。你可以试着在DataGrid控件中只有数据列来看看
      

  2.   

    //自己生成"<table><tr><td>aaa</td></tr></table>"格式吧 好控制
    public void Export()  
    {HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+ExportFileName);
    HttpContext.Current.Response.Charset = "utf-8";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default ;
    HttpContext.Current.Response.ContentType ="application/ms-excel/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/mswordstring strData = "<table border=1>";
    for ( int i=-1; i< this.Items.Count ; i++ )
    {
    strData += "<tr>";
    for ( int j=0; j< this.Columns.Count ; j++ )
    {
    // Igore none visible columns or not .
    if ( !this.Columns[j].Visible && !ExportInvisibleColumns ) continue; strData += "<td>";
    if ( i < 0 ) 
    strData += "<b>"+this.GetHeaderText( j )+"</b>";
    else
    strData += this.GetItemString( i,j );
    strData += "</td>"; }
    strData += "</tr>";
    }
    strData += "</table>";HttpContext.Current.Response.Write( strData );
    HttpContext.Current.Response.End();}public string GetItemString( int Row,int ColumnIndex )
    {
    if ( Row > this.Items.Count ) return "";
    DataGridItem dgItem = this.Items[Row];
    if ( ColumnIndex < 0 || ColumnIndex > dgItem.Cells.Count ) return ""; foreach( Control ctrl in dgItem.Cells[ColumnIndex].Controls )
    {
    if ( ctrl is CheckBox ) return (( CheckBox )ctrl).Checked.ToString();
    if ( ctrl is HyperLink )return (( HyperLink )ctrl).Text;
    if ( ctrl is Label ) return (( Label )ctrl).Text;
    if ( ctrl is Literal ) return (( Literal )ctrl).Text;
    if ( ctrl is TextBox ) return (( TextBox )ctrl).Text;
    }
    return this.Items[ Row ].Cells[ ColumnIndex ].Text;
    }
      

  3.   

    我也出现了类似的问题:类型“DataGridLinkButton”的控件“DataGrid1__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。
      

  4.   

    此中方法是不是用excel来显示datagrid中的数据呀????
      

  5.   

    你的DataGrid是不是有模板列啊!必须先动态去掉
    就好了!