Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to export two gridview in different excel sheet using two different button in c#


C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
      scriptManager.RegisterPostBackControl(this.btnextract);

      ScriptManager scriptManager1 = ScriptManager.GetCurrent(this.Page);
      scriptManager1.RegisterPostBackControl(this.btnpostsap);
   }


   protected void btnextract_Click(object sender, EventArgs e)
   {
      if(btnextract.Text == "Extract Data")
      {
         Response.ClearContent();
         Response.Buffer = true;
         Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Distribution.xls"));
         Response.ContentType = "application/ms-excel";
         System.IO.StringWriter sw = new System.IO.StringWriter();
         HtmlTextWriter htw = new HtmlTextWriter(sw);
         gridview1.AllowPaging = false;
         gridview1.HeaderRow.Font.Bold = true;
         gridview1.HeaderRow.Height = 25;
         gridview1.Columns[0].Visible = false;
         gridview1.RenderControl(htw);
         Response.Write(sw.ToString());
         Response.End();
      }
   }

   protected void btnpostsap_Click(object sender, EventArgs e)
   {
      if(btnpostsap.Text == "Sync Data")
      {
         Response.ClearContent();
         Response.Buffer = true;
         Response.AddHeader("content-disposition", 
         string.Format("attachment; filename={0}", "SyncSapData.xls"));
         Response.ContentType = "application/ms-excel";
         System.IO.StringWriter sw1 = new System.IO.StringWriter();
         HtmlTextWriter htw1 = new HtmlTextWriter(sw1);
         gridview2.AllowPaging = false;
         gridview2.Visible = true;
         gridview2.RenderControl(htw1);
         Response.Write(sw1.ToString());
         Response.End();
      }
   }

   public override void VerifyRenderingInServerForm(Control control)
   {
      /*Tell the compiler that the control is rendered
	explicitly by overriding the VerifyRenderingInServerForm event.*/
   }


i have two button as follows Extract Data and Sync Data.

when i click Extract data button Gridview1 is downloading. But when i click the Sync Data button Gridview2 is not downloading.

what is the mistake in my above postsap click button code.

What I have tried:

[EDIT] copy paste of previous text and code deleted to increase readability[/Edit]
Posted
Updated 10-Aug-18 3:27am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900