Some days ago, a customer asked me about how to email a gridView Control. After some effort, I have solved the problem. First render the GridView to html and then set the html as the email message body.
But how can we render the GridView to html? The following code provide an anwser.

public partial class GridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string RenderControlToHtml(Control c)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
        c.RenderControl(hw);
        return sw.ToString();
    }
    protected override void Render(HtmlTextWriter writer)
    {
        TextBox1.Text = RenderControlToHtml(GridView1);
        base.Render(writer);
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
       
    }
}
here I want to mention the VerifyRenderingInServerForm method, we must override the method inorder to avoid the error message telling you that the server control must be put in the <form runat="server"> tag.

posted on 2007-03-16 16:25  Come on, BABY!  阅读(289)  评论(0)    收藏  举报