当前位置:编程学习 > C#/ASP.NET >>

gridview 导出含有textbox的列时出错.

大家好,在用以下代码导出gridview数据时无法导出在gridview中textbox的内容,只能导出显示出的内容,请问如何处理? 

Response.Clear(); 
  Response.Buffer = true; 
  Response.Charset = "GB2312"; 
    Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); 
    Response.ContentEncoding = System.Text.Encoding.UTF7; 
  Response.ContentType = "application/ms-excel"; 
  System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
  this.GridView1.RenderControl(oHtmlTextWriter); 
    Response.Output.Write(oStringWriter.ToString()); 
  Response.Flush(); 
Response.End(); 
--------------------编程问答-------------------- up --------------------编程问答-------------------- 你是用模板列的吗,在导出前作一个处理:

    /// <summary>
    /// 处理模板列
    /// </summary>
    /// <param name="gv"></param>
    public void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
    {
        Literal l = new Literal();
        string name = String.Empty;
       
        for (int i = 0; i < gv.Controls.Count; i++)
        {          
            if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }

            else if (gv.Controls[i].GetType() == typeof(CheckBox))
            {
                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }

            else if (gv.Controls[i].GetType() == typeof(ImageButton))
            {
                if (gv.Controls[i].Visible == true)
                    l.Text = "SOP";
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }

            else if (gv.Controls[i].GetType() == typeof(Label))
            {
                l.Text = (gv.Controls[i] as Label).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(TextBox))
            {
                l.Text = (gv.Controls[i] as TextBox).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }
--------------------编程问答-------------------- 没搞过!!
帮你顶!! --------------------编程问答--------------------  #region 导出数据

        /// <summary>
        /// 将GridView中的数据原样导出到Excel文件,并且保存到客户端
        /// </summary>
        /// <param name="ctl">需要导出为Excel文件的DataGrid Web控件</param>
        /// <param name="ExportFileName">文件名</param>
        public void GridViewToExcel(System.Web.UI.Control ctl, string ExportFileName)
        {
           // string ExportFileName = MakeExcelFileName(Flag);   //生成文件名

            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8) + ".xls");

            HttpContext.Current.Response.Charset = "GB2312";

            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //System.Text.Encoding.UTF8;

            HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

            ctl.Page.EnableViewState = false;

            System.IO.StringWriter tw = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            this.ClearControls(ctl);

            ctl.RenderControl(hw);

            HttpContext.Current.Response.Write(tw.ToString());

            HttpContext.Current.Response.End();
        }

        private void ClearControls(System.Web.UI.Control control)
        {
            for (int i = control.Controls.Count - 1; i >= 0; i--)
            {
                ClearControls(control.Controls[i]);
            }

            if (!(control is System.Web.UI.WebControls.TableCell))
            {
                if (control.GetType().GetProperty("SelectedItem") != null)
                {
                    System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
                    control.Parent.Controls.Add(literal);
                    try
                    {
                        literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                    }
                    catch
                    {

                    }

                    control.Parent.Controls.Remove(control);
                }

                else

                    if (control.GetType().GetProperty("Text") != null)
                    {
                        System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
                        control.Parent.Controls.Add(literal);
                        literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                        control.Parent.Controls.Remove(control);
                    }
            }
            return;
        } --------------------编程问答-------------------- 报错:
RegisterForEventValidation can only be called during Render(); 


Stack Trace: 


[InvalidOperationException: RegisterForEventValidation can only be called during Render();]
   System.Web.UI.ClientScriptManager.RegisterForEventValidation(String uniqueId, String argument) +2128740
   System.Web.UI.WebControls.TextBox.AddAttributesToRender(HtmlTextWriter writer) +729
   System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer) +17
   System.Web.UI.WebControls.TextBox.Render(HtmlTextWriter writer) +17
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
   System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer) +97
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
   System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +7
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer) +605
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
   System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +7
   System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer, Boolean renderPanel) +202
   System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer) +30
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   ys_all_xm.Button41_Click(Object sender, EventArgs e) +170
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

 
--------------------编程问答-------------------- 大家帮帮忙吧. --------------------编程问答-------------------- 现在程序不报错了,但是导出的Excel文件中textbox的列不能编辑. --------------------编程问答-------------------- --------------------编程问答-------------------- http://blog.csdn.net/qlk_2007/archive/2008/07/18/2669392.aspx
不能编辑试下用Excel设置看看 --------------------编程问答--------------------
1>去除某控件foreach (GridViewRow rw in this.GridView1.Rows)
        {
            ImageButton ch = ((ImageButton)rw.Cells[6].FindControl("ImageButton1"));
            ch.Visible = false;
        }
2>導出某控件的值,但控件不導出for (int i = 0; i < gv.Controls.Count; i++)
        {
            LinkButton lb = new LinkButton();
            Literal l = new Literal();
            string name = String.Empty;
            if (gv.Controls[i].GetType() == typeof(ImageButton))
            {
               
                gv.Controls.Remove(gv.Controls[i]);

            }
     
            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].HasControls())
            {
                DisableControls(gv.Controls[i]);
            }
        }
--------------------编程问答-------------------- 不能导出TextBox。

你先要理解GridView导出Excel文件的原理,其实就是利用TextBox对HTML <table>的兼容性,把整个<table>识别为一个Excel表。如果你的<table>里面有<input>,Excel自然无法处理。 --------------------编程问答-------------------- 你使用文本文件打开看看能不能修改这个文件
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,