0

Created a visual web part for SharePoint 2013 with VS 2012 template. Web part has gridview with paging enable. On the same I have a button for export to excel. Gridview works perfect , but when I click "Export to Excel " that time gridview paging is not working unless I refresh my page. Export to Excel Code for reference

 private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.DataSource = getListData();
        GridView1.DataBind();
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                Table table = new Table();
                if (GridView1.HeaderRow != null)
                {
                    PrepareControlForExport(GridView1.HeaderRow);
                    table.Rows.Add(GridView1.HeaderRow);
                }
                foreach (GridViewRow row in GridView1.Rows)
                {
                    PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
                if (GridView1.FooterRow != null)
                {
                    PrepareControlForExport(GridView1.FooterRow);
                    table.Rows.Add(GridView1.FooterRow);
                }
                table.RenderControl(htw);

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "SiteSize.xls"));
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();

            }
        }
    }
0

This is because of security setting of SharePoint. Source I was able to figure out the issue and solution! All I've done is: added the OnClientClick to the button's event, and called the _spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;.

<asp:Button ID="btnExportToExcel" runat="server" Text="Export to Excel" onclick="btnExportToExcel_Click" OnClientClick="_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.