How to get the HTML name attribute of ASP.Net Control


//Function to get the HTML name of the server control from the Client Id

// Parameters:

//  clientId – Control.ClientId

//  serverId – Control.id

private string GetHTMLNameById(string clientId, string serverId)

{

int pos = serverId.IndexOf(‘_’);

string HTMLName = “”;

if(pos >= 0)

{

pos = clientId.IndexOf(serverId);

HTMLName = clientId.Remove(pos, serverId.Length);

HTMLName = HTMLName.Replace(‘_’, ‘$’);

HTMLName += serverId;

}

else

HTMLName = clientId.Replace(‘_’, ‘$’);

return HTMLName;

}

Courtesy: http://geekswithblogs.net/Gaurav/archive/2006/09/04/90195.aspx

Javascript: String trim functions: leftTrim, rightTrim and trimAll


The first is a definition of the separate function leftTrim, rightTrim and trimAll.
These trim the spaces to the left of the string, the right of the string and both sides respectively.
function leftTrim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    return sString;
}
function rightTrim(sString) 
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
I also noticed that Firefox was working with 'trim', ltrim and rtrim but IE wasn't. 
So I got these functions in and they worked for me in both.

How to generate Random numbers in .NET


Actually these are not exactly ‘true’ random numbers. These are actually pseudo-random numbers, which means that they give you an illusion that they’re being generated randomly.

It’s very simple: There’s a class called System.Random. and the rest is as simple as this:-

Random r = new Random(0);
int num = r.Next(0,999999);

in VB .NET

dim r as Random = new Random(0)
dim num as Int = r.Next(0,999999)

How to make a file download on button click?


Ever wonder how you could put in the functionality of downloading a file when the user clicks the button? Well here’s how I did it. The technique is to send the file to the browser via response and put the content type to application/pdf. Without putting in any further delays, here’s the code.

/// <summary>
/// opens the specified file in the request when provided the correct content type
/// </summary>
/// <param name=”FileName”></param>
/// <param name=”ContentType”></param>
/// <param name=”SaveAsFileName”></param>
/// <param name=”response”></param>

public static void ResponseOpenFile(string FileName, string ContentType, string SaveAsFileName, HttpResponse response)
{

response.Clear();

response.AddHeader(“content-disposition”, “attachment;filename=” + SaveAsFileName);

response.ContentType = ContentType;

response.WriteFile(FileName);

response.End();

}

Thats how I call the method. This is being done in the on click event of the download button.

ResponseOpenFile(Server.MapPath(“../applications/”)+oApp[0].ApplicationID.ToString()+”.pdf”, “application/pdf”, “Application.pdf”, Response);

Happy Programming folks!

Just a little observation about the javascript’s window.open method


well today I was just putting in a pop up window in my application using the Javascript’s window.open method and at first I wrote this:

window.open(“printApp.aspx”,“Print barcode window”,”status=0,width=380,height=175,location=0,scrollbars=0,resizable=0″);

and that was working in Firefox but when I fired up IE to see whats happening there it was giving an invalid parameter’s error. So I started trimming the parameters off one by one and it turned out that there should be no spaces in that second parameter highlighted as bold. 🙂

Generating PDF document in .NET using iTextSharp Library


well, it was just yesterday that I was looking into converting html to pdf programmatically in .NET 2.0. After some googling, I came up to the iTextSharp library at SourceForge.

Just built an application to test run this iTextSharp library. so here’s the code for the “Hello world” application.

private void HelloWorldPdf()
{
try
{
iTextSharp.text.Document oDoc = new iTextSharp.text.Document();
PdfWriter.GetInstance(oDoc, new FileStream(“HelloWorld.pdf”, FileMode.Create));
oDoc.Open();
oDoc.Add(new Paragraph(“Hello World!”));
oDoc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

It works!, Then I tried inserting an image in the pdf document and that worked too.

private void InsertImageinPDF()
{

//Already existing file
string pdfTemplate = “HelloWorld.pdf”;//@”C:\PDFFileSoftware\PdfGenerator_CS\PdfGenerator_CS\PdfGenerator\1_Form SS-4 (2007).pdf”;

//@”c:\Temp\PDF\fw4.pdf”;
string newFile = @”completed_fw4.pdf”;//c:\Temp\PDF\

PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;

string chartLoc = string.Empty;

chartLoc = @”C:\Documents and Settings\Imran Akram\My Documents\My Pictures\TheDeparted.jpg”;//@”C:\Temp\PDF\IMG_3746.jpg”;//pplLogoSmall.jpg”;
iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance(chartLoc);

iTextSharp.text.pdf.PdfContentByte underContent;
iTextSharp.text.Rectangle rect;

try
{
Single X, Y; int pageCount = 0;

rect = pdfReader.GetPageSizeWithRotation(1);
if (chartImg.Width > rect.Width || chartImg.Height > rect.Height)
{

chartImg.ScaleToFit(rect.Width, rect.Height);

X = (rect.Width – chartImg.ScaledWidth) / 2;

Y = (rect.Height – chartImg.ScaledHeight) / 2;

}

else
{

X = (rect.Width – chartImg.Width) / 2;

Y = (rect.Height – chartImg.Height) / 2;

} chartImg.SetAbsolutePosition(X, Y);

pageCount = pdfReader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{

underContent = pdfStamper.GetOverContent(i);//.GetUnderContent(i);

underContent.AddImage(chartImg);

}

pdfStamper.Close();

pdfReader.Close();

}
catch (Exception ex)
{
throw ex;

}

}

OKAY So far so good, but what about the thing I needed the most…. Rendering Html to pdf!! And guess what I was quite disappointed. The HtmlParser is just not strong enough. It gets mad even if it gets a br tag, your code has to be absolutely XHTML conformant or it will give u nothing. But for your interest, I’m giving you guys the code.

/// <summary>
/// Creates the PDF document with a given content at a given location.
/// </summary>
/// <param name=”strFilePath”>The file path to write the new PDF to.</param>
/// <param name=”strContent”>Content in HTML to write to the PDF.</param>
public static void CreatePDFDocument(String strFilePath, String strContent)
{
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strFilePath, FileMode.Create));
//XmlTextReader reader = new XmlTextReader(new StringReader(strContent));
//reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
//HtmlParser.Parse(document, reader);
System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(@”<html><body>This is my <bold>test</bold> string</body></html>”));

HtmlParser.Parse(document, _xmlr);
}
catch (Exception e)
{
throw e;
}
}

I gotta say,  the HtmlParser is really vulnerable. So, I decided to use ABCPdf instead. 🙂

blog for blog


howdy every one,

ah, well, feeling very sleepy right now and I’m just sittin here at my desk upgrading my VS .NET to SP 1. Just heard from my colleague that hey! just one blog in whole September? Just got a deadline tommorow and well here I am sitting with this dumb thing taking so much time So anyway I thought well, atleast there’s some one out there who cares about lookin into my blabbin. So I thought why not write a blog just to blog. So! how’s everyone doing out there han?

Tell me about it!