简体   繁体   English

将Gridview导出到Excel中时出错

[英]Error in Exporting Gridview to Excel

Yeah i have found many question referencing about this but i cant seem to find the exact scenario like i have. 是的,我发现很多与此有关的问题,但我似乎找不到像我一样的确切情况。 so please take a time to look my code below: 因此,请花一些时间在下面查看我的代码:

I have this Script inside a content Place holder 我在内容占位符中包含此脚本

   <script language="javascript" type="text/javascript">
        function GetMac2() {

            var macAddress = "";
            var ipAddress = "";
            var computerName = "";
            var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
            e = new Enumerator(wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
            for (; !e.atEnd(); e.moveNext()) {
                var s = e.item();
                macAddress = s.MACAddress;
            }

            document.getElementById("<% =txtMACAdd.ClientID %>").value = unescape(macAddress);
        }  </script>

and a code for Exporting Gridview to excel 和用于将Gridview导出到excel的代码

HtmlForm form = new HtmlForm();
            Response.Clear();
            Response.Buffer = true;
            string fileName = "Prenda of " + YearDropDownList.SelectedValue + "/" + MonthDropDownList.SelectedValue;
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            PrendaGridView.AllowPaging = false;
            PrendaGridView.DataSource = (DataSet)ViewState["view"];
            PrendaGridView.DataBind();
            form.Controls.Add(PrendaGridView);
            this.Controls.Add(form);
            form.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

The problem is i Recive the error 问题是我收到错误

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

when i include the javascript above, if i remove it. 当我包括上面的JavaScript时,如果我将其删除。 it works well. 它运作良好。 so is there any work around on how to include my javascript? 所以有没有关于如何包含我的JavaScript的解决方法?

i call my java script on page_load like this 我像这样在page_load上调用我的Java脚本

if(!isPostback)
{
   string jvscript = "<script language='javascript'>GetMac2();</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "invoke", jvscript);
}  

Hope you can help me thanks! 希望你能帮我谢谢!

There is one workaround i can think of: 我可以想到一种解决方法:
HTML: HTML:

<input id="tempInp" runat="server" type="hidden"></input>

C#: C#:

tempInp.value = txtMACAdd.ClientID;

Javascript: 使用Javascript:

document.getElementById(document.getElementById("tempInp").value).value = unescape(macAddress);

This will not disturb the javascript and there are no server tags in the javascript so you can do this.Controls.Add(form); 这不会干扰javascript,并且javascript中没有服务器标记,因此您可以执行此操作this.Controls.Add(form);

EDIT: 编辑:
Since you are using server tag to get the Client ID of the control, I think the Client ID and Server ID of the controls are not same. 由于您使用服务器标记来获取控件的客户端ID,因此我认为控件的客户端ID和服务器ID不相同。
There is one more non-standard but simple workaround. 还有另一种非标准但简单的解决方法。 Just copy the Client ID of txtMACAdd from the final source of the page from the browser and paste it in document.getElementById("<% =txtMACAdd.ClientID %>").value . 只需从浏览器的页面最终源复制txtMACAdd的客户端ID, txtMACAdd将其粘贴到document.getElementById("<% =txtMACAdd.ClientID %>").value
Because when we are doing document.getElementById("tempInp").value the tempInp control's ClientID might be different and it will not be able to find the control and again will be the same problem. 因为当我们执行document.getElementById("tempInp").valuetempInp控件的ClientID可能会有所不同,因此它将无法找到该控件,并且再次出现相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM