关于UserControl ... [英] About UserControl ...

查看:94
本文介绍了关于UserControl ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用户控件,其中我有四个图像按钮,如导出到excel,pdf,word,打印文档。我想导出我的基页中的gridview,我从哪里访问此usercontrol现在我的问题是如何发送该gridview的id以及我应该写什么函数来获取我的用户控件中的gridview的id,以便每当任何按钮点击时我都知道哪个网格需要导出?任何解决方案......请帮助...



ascx



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

<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



cs

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

推荐答案

你有两个选择。



首先,您将网格ID初始化为page_load上的属性。用get; set;创建一个公共属性在您的用户控件上并将网格设置为它然后您将拥有它。



第二种方法是处理使用该控件的页面上的控件的单击按钮。如果您需要进一步解释,请在评论中告诉我,我可以为您提供一些代码来改进此解决方案。
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.


这篇关于关于UserControl ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆