Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating a user control in which i have four image buttons like export to excel,pdf,word,print document.I want to export gridview that is in my base page from where i am accessing this usercontrol now my problem is that how to send id of that gridview and what function i should write to get id of that gridview in my user control so that whenever any of the button click fires i get to know which grid i have to export?? Any Solution ...Please Help...

ascx

XML
<div style="float:right;width:auto;" Id="divExport" runat="server" >

    <asp:ImageButton ID="btnWord" ImageUrl="~/images/word_icon.jpg" runat="server"
        onclick="btnWord_Click"/>
    &nbsp;
    <asp:ImageButton ID="btnExcel" ImageUrl="~/images/Excel-icon.png"
        runat="server" onclick="btnExcel_Click" />
    &nbsp;
    <asp:ImageButton ID="btnPdf" ImageUrl="~/images/pdf_icon.jpeg" runat="server"
        onclick="btnPdf_Click" />
    &nbsp;
    <asp:ImageButton ID="btnPrint" ImageUrl="~/images/print.gif" runat="server" />
</div>


cs
C#
protected void btnWord_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.word";
            Response.AddHeader("content-disposition", "attachment;filename=EEMS_Report.doc");
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            divExport.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ContentType = "application/vnd.ms-excel";
            string attachment = "attachment; filename=excel.xls";
            Response.AddHeader("content-disposition", attachment);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            divExport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnPdf_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/pdf";

            Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();

            HtmlTextWriter hw = new HtmlTextWriter(sw);

            divExport.RenderControl(hw);

            StringReader sr = new StringReader(sw.ToString());

            iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A2);

            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

            pdfDoc.Open();

            htmlparser.Parse(sr);

            pdfDoc.Close();

            Response.Write(pdfDoc);

            Response.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Posted
Updated 13-Jun-14 2:54am
v2

1 solution

you have two options.

first, you initialize the grid id as a property on your page_load. just create a public property with get;set; on you user control and set the grid to it and then you will have it.

second way is to handle the control's click buttons on the page that uses the control. let me know on the comments if you need further explanation and I can improve this solution with some code for you.
 
Share this answer
 
Comments
Neha Mukesh 14-Jun-14 0:36am    
Please explain with code ..

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