C# 将Gridview导出到Excel时出错

C# 将Gridview导出到Excel时出错,c#,javascript,asp.net,C#,Javascript,Asp.net,是的,我发现了很多关于这方面的问题,但我似乎找不到像我这样的确切情况。因此,请花点时间查看下面的代码: 我把这个脚本放在一个内容占位符中 <script language="javascript" type="text/javascript"> function GetMac2() { var macAddress = ""; var ipAddress = ""; var compute

是的,我发现了很多关于这方面的问题,但我似乎找不到像我这样的确切情况。因此,请花点时间查看下面的代码:

我把这个脚本放在一个内容占位符中

   <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>
问题是我收到了错误

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

希望你能帮助我,谢谢

我可以想到一个解决方法:
HTML:

<input id="tempInp" runat="server" type="hidden"></input>
tempInp.value = txtMACAdd.ClientID;
document.getElementById(document.getElementById("tempInp").value).value = unescape(macAddress);
Javascript:

<input id="tempInp" runat="server" type="hidden"></input>
tempInp.value = txtMACAdd.ClientID;
document.getElementById(document.getElementById("tempInp").value).value = unescape(macAddress);
这不会干扰javascript,javascript中没有服务器标记,因此您可以执行
This.Controls.Add(form)

编辑:
由于您使用服务器标记获取控件的客户端ID,因此我认为控件的客户端ID和服务器ID不相同。
还有一个非标准但简单的解决方法。只需从浏览器的最终页面源复制
txtMACAdd
的客户端ID,并将其粘贴到
document.getElementById(“”)中。value


因为当我们执行
document.getElementById(“tempInp”).value
时,
tempInp
控件的ClientID可能不同,它将无法找到该控件,并且会再次出现相同的问题。

让我尝试一下。。所以我不会使用输入而不是隐藏字段。。好主意。。我将在完成测试后立即更新您。您可以将其放入
Page_Load()
@p5这正在工作,问题是
tempInp.Value
呈现为空白。这里可能有语法错误
document.getElementById(document.getElementById(“tempInp”).Value=unescape(macAddress)
消息:“document.getElementById(…)”为null或不是对象
我在javascript中收到了错误消息这就是我如何执行的
document.getElementById('ctl00$ContentPlaceholder 1$txtMACAdd')。value=unescape(macAddress)