Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello..

i am doing small web page in which ..i want to send datalist to the mail..

but there are some problems when i send mail..all data will send appropriately but not datalist ..

plz help me.

plz use Northwind database of microsoft..
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<asp:DataList ID="dlContacts" runat="server" RepeatLayout="Table" RepeatColumns="3"
    CellPadding="2" CellSpacing="2">
    <ItemTemplate>
        <table cellpadding="2" cellspacing="0" border = "1" style="width: 200px; height: 100px; border: dashed 2px #04AFEF;background-color: #B0E2F5">
            <tr>
                <td><asp:CheckBox ID="CheckBox1" runat="server" />
                <b><u><%# Eval("ContactName") %></u></b></td>
            </tr>
            <tr>
                <td>
                    <b>City: </b><%# Eval("City") %><br />
                    <b>Postal Code: </b><%# Eval("PostalCode") %><br />
                    <b>Country: </b><%# Eval("Country")%><br />
                    <b>Phone: </b><%# Eval("Phone")%><br />
                    <b>Fax: </b><%# Eval("Fax")%><br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>
<br />
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick = "Print" />
</form>
</body>
</html>

C#
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;

using System.Net.Mail;

public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        dlContacts.DataSource = this.GetData();
        dlContacts.DataBind();
    }
}

private DataTable GetData()
{
    string conString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("select top 6 * from customers"))
        {
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataSet ds = new DataSet();
                sda.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
}
    protected void Print(object sender, EventArgs e)
    {
        DataTable dt = GetData();
        List<datarow> rows = new List<datarow>();
        foreach (DataListItem item in dlContacts.Items)
        {
            if (!(item.FindControl("CheckBox1") as CheckBox).Checked)
            {
                rows.Add(dt.Rows[item.ItemIndex]);
            }
        }
        foreach (DataRow row in rows)
        {
            dt.Rows.Remove(row);
        }
        dlContacts.DataSource = dt;
        dlContacts.DataBind();
        foreach (DataListItem item in dlContacts.Items)
        {
            item.FindControl("CheckBox1").Visible = false;
        }

        SendHTMLMail();
        
    }

    public void SendHTMLMail()
    {
        MailMessage Msg = new MailMessage();
        SmtpClient Smtp;
        MailAddress fromMail = new MailAddress("xyz@com");

        // Sender e-mail address.
        Msg.From = fromMail;
        // Recipient e-mail address.

        Msg.To.Add(new MailAddress("xyz@com"));
        // Subject of e-mail
        Msg.Subject = "Datalist Test";
        Msg.Body += "Please check below data <br /><br />";
        Msg.Body += GetDatalistData(dlContacts);
        
        Msg.IsBodyHtml = true;

        Msg.Priority = MailPriority.High;

        Smtp = new SmtpClient("");
        Smtp.Credentials = new System.Net.NetworkCredential("", "");
        Smtp.Port = 587;
        Smtp.Host = "mail.mwcspune.com";
        Smtp.Send(Msg);
    }

    public string GetDatalistData(DataList dt)
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        dt.RenderControl(hw);
        string html = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type = 'text/javascript'>");
        sb.Append("window.onload = new function(){");
        sb.Append("var printWin = window.open('', '', 'left=0");
        sb.Append(",top=0,width=1000,height=600,status=0');");
        sb.Append("printWin.document.write(\"");
        sb.Append(html);
        sb.Append("\");");
        sb.Append("printWin.document.close();");
        sb.Append("printWin.focus();");
        sb.Append("printWin.print();printWin.opener.location.href=printWin.opener.location.href;");
        sb.Append("printWin.close();};");
        sb.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
        return sb.ToString();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
}
Posted
v3

1 solution

Use this method

C#
public static string RenderControl(Control ctrl)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ctrl.RenderControl(hw);
        return sb.ToString();
    }


and call function as
C#
Msg.Body += RenderControl(datalist1);

Hope it will resolve your problem.
 
Share this answer
 
Comments
Dixit Ashish 9-Jan-14 2:19am    
yess sir...!!

working.. thanks a lot..!!

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