Archive

Archive for December, 2014

Export GridView to PDF and send PDF File as email attachment in ASP.Net

December 23, 2014 Leave a comment

Dear all,

try this example to Export GridView to PDF and send PDF File as email attachment in ASP.Net

1- Open VS 2010 and create a new Web Site

2- Add new Web Page and from Toolbox drag Gridview and Button control

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" ItemStyle-Width="80" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<asp:Button ID="btnExport" runat="server" Text="Export To PDF and Send email" OnClick="ExportToPDF" />

3- You will need to import the following namespaces

using System.IO;
using System.Data;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

4- In Code behind add the following code


protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Code"), new DataColumn("Name"), new DataColumn("City") });
dt.Rows.Add(1, "Yasser Zaid", "Cairo");
dt.Rows.Add(2, "Ahmed Zaid", "Alexandria");
dt.Rows.Add(3, "Ali Mohamed", "Cairo");
dt.Rows.Add(4, "Sameh Saif", "Cairo");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

protected void ExportToPDF(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = "GridView Exported PDF";
mm.Body = "GridView Exported PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "ExportedGridViewPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<passord>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}

public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

Hope this helps

Good Luck.

Categories: ASP.Net

Bind Hours, Minutes and Seconds in ASP.net DropDownList

December 14, 2014 Leave a comment

Hi all,

try this example to Bind Hours, Minutes and Seconds in ASP.net DropDownList

<div>
<fieldset style="width:330px;">
<legend>Populate Hour,Minute and Seconds in DropDownList</legend>Hours:
<asp:DropDownList ID="ddlHours" runat="server">
</asp:DropDownList>
Minutes:
<asp:DropDownList ID="ddlMinutes" runat="server">
</asp:DropDownList>
Seconds:
<asp:DropDownList ID="ddlSeconds" runat="server">
</asp:DropDownList>
</fieldset>
</div>

In Code behind :

 protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateHours();
PopulateMinutes();
PopulateSeconds();
}
}
private void PopulateHours()
{
for (int i = 0; i <= 24; i++)
{
ddlHours.Items.Add(i.ToString("D2"));
}
}
private void PopulateMinutes()
{
for (int i = 0; i < 60; i++)
{
ddlMinutes.Items.Add(i.ToString("D2"));
}
}
private void PopulateSeconds()
{
for (int i = 0; i < 60; i++)
{
ddlSeconds.Items.Add(i.ToString("D2"));
}
}

Hope this helps

Good Luck.

Categories: ASP.Net

Validating a ListBox Using Required Field Validator in Asp.Net

December 9, 2014 Leave a comment

Hi all,

Try this example to Validate a ListBox Using Required Field Validator in Asp.Net

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ListBox ID = "ListBox1" runat = "server" Width="200" Height="200" SelectionMode = "Multiple">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator ID = "RequiredFieldValidator1" ControlToValidate ="ListBox1" InitialValue = "" runat="server" ErrorMessage = "*" ForeColor="Red"></asp:RequiredFieldValidator>


<hr />
<asp:Button ID="Button1" runat="server" Text="Save" />
</asp:Content>

Hope this helps

Good Luck.

Categories: ASP.Net

Create Comma Separated list of CheckBox Values

December 8, 2014 Leave a comment

Hi all,

try this example to Create Comma Separated list of CheckBox Values

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="Text1" Value="1"></asp:ListItem>
<asp:ListItem Text="Text1" Value="2"></asp:ListItem>
<asp:ListItem Text="Text1" Value="3"></asp:ListItem>
<asp:ListItem Text="Text1" Value="4"></asp:ListItem>
<asp:ListItem Text="Text1" Value="5"></asp:ListItem>
<asp:ListItem Text="Text1" Value="6"></asp:ListItem>
</asp:CheckBoxList>


<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

In Code behind :


using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

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

}

protected void Button1_Click(object sender, EventArgs e)
{
Collection<string> items = new Collection<string>();
for (int index = 0; index < CheckBoxList1.Items.Count; index++)
{
if (CheckBoxList1.Items[index].Selected)
{
items.Add(CheckBoxList1.Items[index].Value);
}
}
//here you will get selected values
Label1.Text = String.Join(",", items.ToArray());
}
}
}

Hope this helps

Good Luck.

Categories: ASP.Net

Validate Minimum Length of Textbox

December 5, 2014 Leave a comment

Hi all,

try this example to Validate Minimum Length of Textbox using ASP.Net Validation Control

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="valUserName" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="Minimum length 6 characters" ForeColor="" ValidationExpression=".{6}.*" ></asp:RegularExpressionValidator>

Hope this helps

Good Luck.

Categories: ASP.Net