简体   繁体   中英

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

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. it works well. so is there any work around on how to include my javascript?

i call my java script on page_load like this

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:

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

C#:

tempInp.value = txtMACAdd.ClientID;

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);

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.
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 .
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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