Archive

Archive for January, 2013

Count Down Timer on Page

January 20, 2013 Leave a comment

Hi all,

try this example to create Count Down Timer on Page

1) Open VS 2008 and create a new website and add web page to be like this


<form id="form1" runat="server">
 <div>
 <span>
 <script type="text/javascript" language="JavaScript">
 TargetDate = '<%= AplicationBase() %>';
 NowDate = '<%= NowDate() %>';
 BackColor = "palegreen";
 ForeColor = "navy";
 CountActive = true;
 CountStepper = -1;
 LeadingZero = true;
 DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
 FinishMessage = "It is finally here!";
 </script>
 <script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script
 </span>
 </div>
</form>

and in code behind add the following code :


protected string AplicationBase()
 {
 DateTime TD = DateTime.Now.AddDays(4);
 return TD.ToString();
 }
 protected string NowDate()
 {
 DateTime ND = DateTime.Now;
 return ND.ToString();
 }

for more information check this link

Hope this helps

Good Luck.

Categories: ASP.Net, Javascript

How to Convert DataTable to PDF using ITextSharp

January 20, 2013 4 comments

Hi all,

try this example to  Convert DataTable to PDF using ITextSharp

In this example i’m using ITextSharp (V 4.1.2)

1) Open VS 2008 and create a new website

2) Add new Web.Config file and add connection string to our database and in this example i will use Northwind

<connectionStrings>
<add name="conn" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
 providerName="System.Data.SqlClient" />
 </connectionStrings>

3) Add new Web Page and in code behind add the following code :


private DataTable GetData()
 {
 DataTable dt = new DataTable();
 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
 SqlCommand comm = new SqlCommand("select * from Products", conn);
 SqlDataAdapter adpter = new SqlDataAdapter(comm);
 adpter.Fill(dt);
 return dt;
 }

public void ExportToPdf(DataTable myDataTable)
 {
 Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
 try
 {
 PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);

//-- Add footer ----------------
 HeaderFooter footer = new HeaderFooter(new Phrase(new Phrase("This is page: ", new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA,7, iTextSharp.text.Font.NORMAL))), true);
 pdfDoc.Footer = footer;
 //--- Add Header --------------
 HeaderFooter header = new HeaderFooter(new Phrase("Image scanned on " + DateTime.Now.ToString("dd/MM/yyyy") + " by " + Environment.UserName), false);
 pdfDoc.Header = header;
 //------------------------------
 pdfDoc.Open();
 Chunk c = new Chunk("Yasser Zaid " , FontFactory.GetFont("Verdana", 11));
 Paragraph p = new Paragraph();
 p.Alignment = Element.ALIGN_CENTER;
 p.Add(c);
 pdfDoc.Add(p);
 //--- Add Logo of PDF ----
 string imageFilePath = System.Web.HttpContext.Current.Server.MapPath("images/1.jpg");
 iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
 //Resize image depend upon your need
 jpg.ScaleToFit(80f, 60f);
 //Give space before image
 jpg.SpacingBefore = 0f;
 //Give some space after the image
 jpg.SpacingAfter = 1f;
 jpg.Alignment = Element.HEADER;
 pdfDoc.Add(jpg);
 iTextSharp.text.Font font8 = FontFactory.GetFont("ARIAL", 7);
 //--- Add new Line ------------
 Phrase phrase1 = new Phrase(Environment.NewLine);
 pdfDoc.Add(phrase1);
 //-------------------------------
 DataTable dt = myDataTable;
 if (dt != null)
 {
 //---- Add Result of DataTable to PDF file With Header -----
 PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
 pdfTable.DefaultCell.Padding = 3;
 pdfTable.WidthPercentage = 100; // percentage
 pdfTable.DefaultCell.BorderWidth = 2;
 pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

foreach (DataColumn column in dt.Columns)
 {
 pdfTable.AddCell(FormatHeaderPhrase(column.ColumnName));
 }
 pdfTable.HeaderRows = 1; // this is the end of the table header
 pdfTable.DefaultCell.BorderWidth = 1;

foreach (DataRow row in dt.Rows)
 {
 foreach (object cell in row.ItemArray)
 {
 //assume toString produces valid output
 pdfTable.AddCell(FormatPhrase(cell.ToString()));
 }
 }
 pdfDoc.Add(pdfTable);
 }
 pdfDoc.Close();
 Response.ContentType = "application/pdf";
 Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
 System.Web.HttpContext.Current.Response.Write(pdfDoc);
 Response.Flush();
 Response.End();
 }
 catch (DocumentException de)
 {
 System.Web.HttpContext.Current.Response.Write(de.Message);
 }
 catch (IOException ioEx)
 {
 System.Web.HttpContext.Current.Response.Write(ioEx.Message);
 }
 catch (Exception ex)
 {
 System.Web.HttpContext.Current.Response.Write(ex.Message);
 }
 }

private Phrase FormatPhrase(string value)
 {
 return new Phrase(value, FontFactory.GetFont(FontFactory.TIMES, 8));
 }

private static Phrase FormatHeaderPhrase(string value)
 {
 return new Phrase(value, FontFactory.GetFont(FontFactory.TIMES, 8, iTextSharp.text.Font.UNDERLINE, new iTextSharp.text.Color(0, 0, 255)));
 }

protected void Page_Load(object sender, EventArgs e)
 {
 ExportToPdf(GetData());
 }

Don’t forget to use the following Namespace


using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Hope this helps

Good Luck.

Categories: ASP.Net

Bind TreeView from Database using ADO.Net

January 16, 2013 Leave a comment

Hi all,

try this example to Bind TreeView from Database using ADO.Net

1) Open MS SQL Server 2005 and create new Database

2) Add two Table ParentTable and ChildTable with the following coloumns as below

Table2

Table1

3) Open MS VS 2008 and create a new website and add new web page

4) From ToolBox add TreeView control to your page and in code behind add the following code:


protected void Page_Load(object sender, EventArgs e)
{
fillParent();
}

public void fillParent()
{
DataSet PrSet = PDataset("Select * from ParentTable");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["ParentName"].ToString();
tnParent.Value = dr["ParentID"].ToString();
tnParent.PopulateOnDemand = true;
tnParent.ToolTip = "Click to get Child";
tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Expand();
tnParent.Selected = true;
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, tnParent.Value);
}
}

public void FillChild(TreeNode parent,string ParentId)
{
DataSet ds = PDataset("Select * from ChildTable where ParentId =" + ParentId);
parent.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["ChildName"].ToString().Trim();
child.Value = dr["ChildId"].ToString().Trim();
if (child.ChildNodes.Count == 0)
{
child.PopulateOnDemand = true;
}
child.ToolTip = "Click to get Child";
child.SelectAction = TreeNodeSelectAction.SelectExpand;
child.CollapseAll();
parent.ChildNodes.Add(child);
}
}

protected DataSet PDataset(string Select_Statement)
{
SqlConnection SqlCon = new SqlConnection(ConnectionString here);
SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, SqlCon);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}

after that open your page it will be like this after binding data

Image1

Hope this helps

Good Luck.

Categories: ASP.Net

Display Current Time without Refreshing Page using Javascript

January 8, 2013 Leave a comment

Hi all,

try this example to Display Current Time without Refreshing Page using JavaScript


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JavaScript display current time on webpage</title>
<script type="text/javascript">
function ShowCurrentTime() {
var dt = new Date();
document.getElementById("lblTime").innerHTML = dt.toLocaleTimeString();
window.setTimeout("ShowCurrentTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
}
</script>
</head>
<body onload="ShowCurrentTime()">
<form id="form1" runat="server">
<div>
JavaScript Display current time second by second.
<label id="lblTime" style=" font-weight:bold"></label>
</div>
</form>
</body>
</html>

Hope this helps

Good Luck.

Categories: ASP.Net, Javascript

Disable Past Dates in AJAX Calender

January 3, 2013 Leave a comment

Hi all,

In previous post i show you how to allow user to select only first date of month

Allow User to Select only First day of month in AJAX Calender

in this post i will show how to prevent user to select past dates in AJAX Calender

1) Open VS2008 and create a new website

2) Add new Page and from Toolbox drag ScriptManager , TextBox and AJAX Calender Controls.

3) Add This Javascript function


<script type="text/javascript">
 function checkDate(sender, args) {
 if (sender._selectedDate < new Date()) {
 alert("You cannot select a day earlier than today!");
 sender._selectedDate = new Date();
 // set the date back to the current date
 sender._textbox.set_Value(sender._selectedDate.format(sender._format))
 }
 }
 </script>

4) Your page will be like this


<asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
 Please Select Date
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:CalendarExtender ID="CalendarExtender1" BehaviorID="CalendarExtender1" runat="server"
 TargetControlID="TextBox1" PopupButtonID="TextBox1" OnClientDateSelectionChanged="checkDate">
 </asp:CalendarExtender>

Hope this helps

Good Luck

Categories: AJAX, ASP.Net

Disable future Date in JQuery Calender

January 3, 2013 1 comment

Hi all,

try this example to Disable future Date in JQuery Calender

1) Create a new website and add new Page

2) Inside your page add the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
 <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css"
 type="text/css" media="all" />
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"
 type="text/javascript"></script>
 <script type="text/javascript">
 $(function () {
 var date = new Date();
 var currentMonth = date.getMonth();
 var currentDate = date.getDate();
 var currentYear = date.getFullYear();

$('#<%= txtDate.ClientID %>').datepicker({
 maxDate: new Date(currentYear, currentMonth, currentDate)
 });
 });
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
 </form>
</body>
</html>

Hope this helps

Good Luck

Categories: ASP.Net, Jquery