Wednesday, August 26, 2009

Itextsharp place footer at one page and at bottom

iTextSharp.text.Image footerImage = new Quote().GetFooter();

Rectangle page = document.PageSize();

PdfPTable foot = new PdfPTable(1);
foot.DefaultCell.BorderWidth = 0;
foot.DefaultCell.BorderColorBottom = new Color(255, 255, 255);

foot.AddCell(footerImage);

foot.TotalWidth = page.Width - document.LeftMargin - document.RightMargin;

foot.WriteSelectedRows(0, -1, document.LeftMargin, foot.TotalHeight - 7, writer.DirectContent);

Friday, August 7, 2009

Drag and Drop table row

http://www.isocra.com/2007/07/dragging-and-dropping-table-rows-in-javascript/#demo -- Nice demo for drag and drop Table rows.

Monday, May 18, 2009

js function convert number to money/currency format

function num2money(n_value) {

// validate input
if (isNaN(Number(n_value)))
return 'ERROR';

// save the sign
var b_negative = Boolean(n_value < 0);
n_value = Math.abs(n_value);

// round to 1/100 precision, add ending zeroes if needed
var s_result = String(Math.round(n_value*1e2)%1e2 + '00').substring(0,2);

// separate all orders
var b_first = true;
var s_subresult;
while (n_value > 1) {
s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);
s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
b_first = false;
n_value = n_value/1e3;
}
// add at least one integer digit
if (b_first)
s_result = '0.' + s_result;

// apply formatting and return
return b_negative
? '($' + s_result + ')'
: '$' + s_result;
}

Friday, April 10, 2009

XML File Generation

-- Generate Parent Element
if(!File.Exist(Path))
{
XmlTextWriter writer = new XmlTextWriter(Path);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("ElementName");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
-- Generate Child Element
XmlDocument xmldoc = new XmlDocument();
XMLNode root = xmldoc.DocumentElement;
xmldoc.Load(path);
XmlElement ChildInfo = xmldoc.CreateElement("ChildElement");
ChildInfo.InnerText = "Test";
root.AppendChild(childInfo);

Asp Net Service setup

You can create windows service functionality usinig aspnet service.
On Application_Start Event you have to register cacheitem like below:-
private const string DummyCacheItemKey = "TestKey";
void Application_Start(object sender, EventArgs e)
{
RegisterCacheEntry();
}

private void RegisterCacheEntry()
{
// Prevent duplicate key addition
if (null != HttpContext.Current.Cache[DummyCacheItemKey]) return;

HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null, DateTime.MaxValue,
TimeSpan.FromMinutes(1), CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(CacheItemRemovedCallback));
}
public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{


// Do the service works
DoWork();

// We need to register another cache item which will expire again in one
// minute. However, as this callback occurs without any HttpContext, we do not
// have access to HttpContext and thus cannot access the Cache object. The
// only way we can access HttpContext is when a request is being processed which
// means a webpage is hit. So, we need to simulate a web page hit and then
// add the cache item.
HitPage();
}
private void DoWork()
{

//task that you want to perform;
}

private void HitPage()
{
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadData(Application["AppUrl"].ToString()+ "/DummyForm.aspx");
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
System.Web.HttpApplication app = (System.Web.HttpApplication)sender;
string url = app.Request.Url.AbsoluteUri.ToString();
if (url.IndexOf("DummyForm.aspx") != -1)
{
// Add the item in cache and when succesful, do the work.
RegisterCacheEntry();
}
}

Thursday, February 26, 2009

JS for Paging HTML

function PagingHTML(PageIndex,PageSize,TotalRecords,LastRecord,fnName){
var strHTML = "<table width=100% border=\"0\"><tbody><tr style=\"font-weight: bold;\"><td><div style='float:right;'>";
var j = parseInt(TotalRecords / PageSize);
if (TotalRecords % PageSize != 0)
j++;
i = parseInt((PageIndex - 1) / 10) * 10 + 1;
if ((j - i) < 10 && j > 10)
i = j - 9;
var k;
if (PageIndex > 10)
strHTML += "<div style='float:left;padding-left:5px;'><a class=\"Paging\" href=\"javascript:"+fnName+"(" + (i - 1) + ")\"> ... </a></div>";
for (k = 1; i <= j && k <= 10; i++, k++){
if (i == PageIndex)
strHTML += "<div style='float:left;padding-left:5px;' class=\"Paging\">" + i + "</div>";
else
strHTML += "<div style='float:left;padding-left:5px;'><a class=\"Paging\" href=\"javascript:"+fnName+"(" + i + ")\">" + i + "</a></div>";
}
if (j >= i)
strHTML += "<div style='float:left;padding-left:5px;'><a class=\"Paging\" href=\"javascript:"+fnName+"(" + i + ")\"> ... </a></div>";
strHTML += "</div><div class=\"Paging\">Showing " + (((PageIndex - 1) * PageSize) + > + " - " + (((PageIndex - 1) * PageSize) + LastRecord) + " of " + TotalRecords + "</div>";
strHTML += "</td></tr></tbody></table>";
return strHTML;
}

Monday, February 23, 2009

Javascript for drag an htmll element

Javascript
function startdrag(t, e) {
if (e.preventDefault) e.preventDefault(); //line for IE compatibility
e.cancelBubble = true;
window.document.onmousemoveOld = window.document.onmousemove;
window.document.onmouseupOld = window.document.onmouseup;
window.document.onmousemove=dodrag;
window.document.onmouseup=stopdrag;
window.document.draged = t;
t.dragX = e.clientX;
t.dragY = e.clientY;
return false;
}

function dodrag(e) {

if (!e) e = event; //line for IE compatibility
t = window.document.draged;
t.style.left = (t.offsetLeft + e.clientX - t.dragX)+"px";
t.style.top = (t.offsetTop + e.clientY - t.dragY)+"px";
t.dragX = e.clientX;
t.dragY = e.clientY;
return false;
}
//restore event-handlers
function stopdrag() {
window.document.onmousemove=window.document.onmousemoveOld;
window.document.onmouseup=window.document.onmouseupOld;
}

HTML
<DIV
style="width:100px;height:100px;background:#EEEEEE;border:1px solid black"
><table style="position:absolute;border-color: black; border-style: solid; border-width: 1px" width="20%" onmousedown="startdrag(this, event);"><tr><td>Darg Me</td></tr></table></DIV>