May 2009 - Posts

Just check out microsoft.com/areyoucertifiable

a funny dummy test environment who wants to appear and test yourself for ms certification. You can create login or can play as guest, select avtar. There are more than 400 Questions (objective - options to select) with 20 episodes and 5 seasons.

lol.. i loved this :)

The default behaviour of AJAX Accordion is: you click on any Accordion pane (say; any control placed in Accorion Pane Header section) cause that pane to Expand and collapse rest of the panes other than the pane which you clicked.

It is also fairly easy to open Accordion Panes when you "MouseOver" on Pane rather than need to click the pane to open it.

All you need to add a JavaScript function to ahieve that and call it or "MouseOver" event of ImageButton / Link / or any submit control which is placed inside Accordion Pane.

You need to add below script in your page:

    <script language="javascript">
        function Openpane(paneIndex)
        {
            //MyAccordion is the ID of AJAX Accordion control
            var behavior = $get("<%=MyAccordion.ClientID%>").AccordionBehavior;
            behavior.set_SelectedIndex(paneIndex);
        }       
    </script>

You need to pass the current Accordion Pane index as asrument of the above script function and call this function on "MouseOver" event as:

            <Panes>
                <ajaxToolkit:AccordionPane ID="AccordionPane1" runat="server">
                    <Header>
                        <a href="" class="accordionLink" onmouseover="Openpane('0')">1. Accordion</a></Header>
                    <Content>
                        The Accordion is a web control that allows you to provide multiple panes and display
                        them one at a time. It is like having several Panes where only one can be expanded
                        at a time. The Accordion is implemented as a web control that contains AccordionPane
                        web controls. Each AccordionPane control has a template for its Header and its Content.
                        We keep track of the selected pane so it stays visible across postbacks.
                    </Content>
                </ajaxToolkit:AccordionPane>
            </Panes>

for every pane, you can call the script function on "MouseOver" event by passing the index of that pane as argument.

Thats it! Hope it would be helpful for someone in same need.

I need to add Captcha Control for couple of contact and submission forms to stop possible spam bots. I tried with the similar way I approched In previous website application developed in ASP.NET. But, Its even too easy to add Captcha capability in forms than we do for simple websites. DotNetNuke/DNN is having a built in Captcha control which can be used on contact and submission forms.

First you need to add register tag for the control on your module user control. This will allow to create Captcha control and use it in code. The register line goes as:

<%@ Register TagPrefix="dnn" Assembly="DotNetNuke" Namespace="DotNetNuke.UI.WebControls"%>

Next, is you need to create the control in ascx page as:

<dnn:CaptchaControl runat="server" ID="dnnCaptchaControl" ErrorStyle-CssClass="NormalRed" cssclass="Normal" ErrorMessage="The typed code must match the image, please try again" CaptchaHeight="35" CaptchaWidth="120" />

Believe me, you are done with DNN Captcha control! You only need to validate whether the entered value for captcha by user is correct or not. You can write below code in contact/sybmission form's submit button click event:

if(ctlCaptcha.IsValid)
{
   //Captcha is valid! Code to submit the contact/submission form...
}

else

  return;

This is a mere note to remember for future rather than a blog entry. While I am working on code to download file from server. I write below code:

        {
int ChunkSize = 10000;
string sFileFullPath = Server.MapPath("New Text Document.txt");
System.IO.FileInfo toDownload = new System.IO.FileInfo(sFileFullPath);
if (System.IO.File.Exists(sFileFullPath))
{
Response.Clear();
using (FileStream iStream = System.IO.File.OpenRead(sFileFullPath))
{
long dataLengthToRead = iStream.Length;
Byte[] buffer = new Byte[dataLengthToRead];
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
while (ChunkSize > 0 && Response.IsClientConnected)
{
if (ChunkSize > dataLengthToRead)
{
ChunkSize = int.Parse(dataLengthToRead.ToString());
}
int lengthRead = iStream.Read(buffer, 0, ChunkSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
Response.Close();
Response.End();
}
else
{
this.Page.ClientScript.RegisterStartupScript(GetType(), "ShowMessage", "<script language='javascript'>alert('No Files Available');</script>");
}
}

When I ran the application I found that the file which I need to download was containing spaces in its name like “New Text Document.txt”.

 

The problem is:

In FF, was truncating the filename from space while showing the file download dialog . So, its only showing the filename as “New” (and truncating the rest of the filename after space).

In IE, was replacing the spaces in filename with underscore ( _ ) as “New_Text_Document.txt” while showing the file download dialog box.

This something I dont want to have. I need to have the exact filename as it is on server.

 

The solution is:

For FF: you need to enclose the name of the file in Quotes while you add it as attachment in response header as:

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

 

For IE: you need to identify and replace the space in filename with “%20” (something like encoding filename) as:

String userAgent = Request.Headers.Get("User-Agent");
String filename = toDownload.Name;
if (userAgent.Contains("MSIE 7.0"))
filename = toDownload.Name.Replace(" ", "%20");

Thats It! Hope it would be helpful for someone in same need!

Complete Code would be:

 

        {
int ChunkSize = 10000;
string sFileFullPath = Server.MapPath("New Text Document.txt");
System.IO.FileInfo toDownload =
new System.IO.FileInfo(sFileFullPath);
if (System.IO.File.Exists(sFileFullPath))
{
Response.Clear();
using (FileStream iStream = System.IO.File.OpenRead(sFileFullPath))
{
long dataLengthToRead = iStream.Length;
Byte[] buffer = new Byte[dataLengthToRead];
Response.ContentType = "application/octet-stream";
String userAgent = Request.Headers.Get("User-Agent");
String filename = toDownload.Name;
if (userAgent.Contains("MSIE 7.0"))
filename = toDownload.Name.Replace(" ", "%20");
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
while (ChunkSize > 0 && Response.IsClientConnected)
{
if (ChunkSize > dataLengthToRead)
{
ChunkSize = int.Parse(dataLengthToRead.ToString());
}
int lengthRead = iStream.Read(buffer, 0, ChunkSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
Response.Close();
Response.End();
}
else
{
this.Page.ClientScript.RegisterStartupScript(GetType(), "ShowMessage", "<script language='javascript'>alert('No Files Available');</script>");
}
}

I am trying to validate textbox for numeric entry, while I added this code to accomplish in page_load event:

textbox.Attributes.Add("onkeypress", "return (window.event.keyCode == 45 || window.event.keyCode == 13 || window.event.keyCode == 8 || window.event.keyCode == 9 || window.event.keyCode == 189 || window.event.keyCode == 109 || (window.event.keyCode >= 48 && window.event.keyCode <= 58) )");

It worked fine in IE7 and IE8, but it didnt work for FF though. A bit googling gave me conclusion that FF doest recognize window.event.keyCode. Basically Firefox supports the which property instead of the keyCode property IE does. By checking which (window.event.which) of them exists with an if statement we can reliably get the key that was pressed.

I replaced above code with:

textBox.Attributes.Add("onkeypress", "var key; if(window.event){ key = event.keyCode;}else if(event.which){ key = event.which;} return (key == 45 || key == 13 || key == 8 || key == 9 || key == 189 || (key >= 48 && key <= 58) )");

it worked for both IE and FF. Hope it would help someone in same need!

 

Here is another requirement (perhaps a common requirement!) I needed to implement in one of on-going project website application. I needed to generate a PDF file as output from the inputs entered by end-user in a form. On Completing the form, end-user should be able to download a PDF file of what they have given the inputs in the form.

I need to generate PDF file in a given format and force the download dialog to let the user save generated PDF save on their/client machine.

I tried searching any free :) third party dlls which would help me to generate PDF files on-the-fly. Things I tried:

[1] Tried searching almost third party tools/dlls including Siberix PDF Library , PDF Vision .Net etc etc, which are having similar functionality. But not FREE :(

[2] Tried with iTextSharp - Tutorial and PDFsharp - Download PDFsharp Version 1.20 These dlls/libraries are totally free and really interesting to work with. All we need to do is define elements like HTML and add those elements to PDF document object which then finally generated as PDF document. But, this is the problem for me as I am having a huge PDF file needed to generate as output and it would required a loot of time to code for a document by placing line by line cells, TR and TD, Tables and background, Border colors, images etc. and most of all is annoying Texts to be placed in those elements with proper fonts, colors and padding/alignment.

At last after wasting time in googling and on free tools/libraries, I got an idea to create PDF on-the-fly. I was thinking of creating PDF files dynamically and I created it smoothly with out too much work and code.

This is how it goes:

[1] I created a clean A4 size SnapShot of the PDF file I needed to be generated dynamically.

[2] Next, I added a blank crystal report to the solution.

[3] I added/set the PDF SnapShot as the background of the crystal report by Right Click on the detail section of the crystal report > Insert > Picture > and select the SnapShot and place it inside the detail section with proper left-top padding.

[4] as of now, Crystal Report is showing exactly same as the PDF file with blank inputs.

[5] next is, to accept inputs from end-user and pass those inputs as parameters to Crystal Report.

[6 ] I declared required parameters those are needed to pass runtime with the input values entered by end-user to Crystal Report. And place those parameters object in the Crystal Report accurately in a way, like those parameters were showing as the resultant values in PDF files. At this time, Crystal Report is having PDF like look with parameters. For this I need to set the PDF SnapShot background "send to BACK" as:

Below are the declared parameters which will be passed dynamically after accepting/submitting user inputs.

This is how Crystal Report saw when Parameters placed on the Crystal Report Detailed section after moving the SnapShot to move BACK:

 

[7] After Declaring parameters, only thing left is to pass values and directly export the Crystal Report in PDF file rather than to show it on user screen. And after successful Export in PDF file, force download dialog to let end-user save the generated PDF file.

 

Below is the Code to pass parameters dynamically and force download dialog to save the file:

'create the report document

Dim doc As New ReportDocument()

Dim fileName As String = Server.MapPath("~/DesktopModules/OnlineForm/OnlineForm.rpt")

'load the rpt file to document

doc.Load(fileName)

'pass the user inputs to crystal report parameters those will sit in the output pdf file

doc.SetParameterValue("@CompanyName", txtCompany.Text.Trim())

doc.SetParameterValue("@CustomerName", txtFirstName.Text.Trim() + " " + txtLastName.Text.Trim())

doc.SetParameterValue("@Address", txtAddress.Text.Trim())

doc.SetParameterValue("@City", txtCity.Text.Trim())

doc.SetParameterValue("@Province", txtProvince.Text.Trim())

doc.SetParameterValue("@PostalCode", txtPostalCode.Text.Trim())

doc.SetParameterValue("@Phone1", txtPhone11.Text.Trim() + " - " + txtPhone12.Text.Trim() + " - " + txtPhone13.Text.Trim())

doc.SetParameterValue("@Phone2", txtPhone21.Text.Trim() + " - " + txtPhone22.Text.Trim() + " - " + txtPhone23.Text.Trim())

doc.SetParameterValue("@Phone3", txtPhone31.Text.Trim() + " - " + txtPhone32.Text.Trim() + " - " + txtPhone33.Text.Trim())

'set the export options to PDF

Dim exportOpts As ExportOptions = doc.ExportOptions

exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat

exportOpts.ExportDestinationType = ExportDestinationType.DiskFile

exportOpts.DestinationOptions = New DiskFileDestinationOptions()

' Set the disk file options.

Dim diskOpts As New DiskFileDestinationOptions()

CType(doc.ExportOptions.DestinationOptions, DiskFileDestinationOptions).DiskFileName = Server.MapPath("~/DesktopModules/OnlineForm/OnlineForm.pdf")

'export the report to PDF rather than displaying the report in a viewer

doc.Export()

'force download dialog to download the PDF file at user end.

'Set the appropriate ContentType.

Response.ContentType = "Application/pdf"

'Get the physical path to the file.

Dim FilePath As String = Server.MapPath("~/DesktopModules/OnlineForm/OnlineForm.pdf")

'Write the file directly to the HTTP content output stream.

Response.WriteFile(FilePath)

Response.End()

 

Thats it!

Hope it would help anybody who need to develop similar functionality!

 

UPDATE:

incase, if anybody needs code in C#:

 

//create the report document
ReportDocument doc = new ReportDocument();
string fileName = Server.MapPath("~/DesktopModules/OnlineForm/OnlineForm.rpt");
//load the rpt file to document
doc.Load(fileName);

//pass the user inputs to crystal report parameters those will sit in the output pdf file
doc.SetParameterValue("@CompanyName", txtCompany.Text.Trim());
doc.SetParameterValue("@CustomerName", txtFirstName.Text.Trim() + " " + txtLastName.Text.Trim());
doc.SetParameterValue("@Address", txtAddress.Text.Trim());
doc.SetParameterValue("@City", txtCity.Text.Trim());
doc.SetParameterValue("@Province", txtProvince.Text.Trim());
doc.SetParameterValue("@PostalCode", txtPostalCode.Text.Trim());
doc.SetParameterValue("@Phone1", txtPhone11.Text.Trim() + " - " + txtPhone12.Text.Trim() + " - " + txtPhone13.Text.Trim());
doc.SetParameterValue("@Phone2", txtPhone21.Text.Trim() + " - " + txtPhone22.Text.Trim() + " - " + txtPhone23.Text.Trim());
doc.SetParameterValue("@Phone3", txtPhone31.Text.Trim() + " - " + txtPhone32.Text.Trim() + " - " + txtPhone33.Text.Trim());

//set the export options to PDF
ExportOptions exportOpts = doc.ExportOptions;
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
exportOpts.DestinationOptions = new DiskFileDestinationOptions();

// Set the disk file options.
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
((DiskFileDestinationOptions)doc.ExportOptions.DestinationOptions).DiskFileName = Server.MapPath("~/DesktopModules/OnlineForm/OnlineForm.pdf");
//export the report to PDF rather than displaying the report in a viewer
doc.Export();

//force download dialog to download the PDF file at user end.
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = Server.MapPath("~/DesktopModules/OnlineForm/OnlineForm.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();

Core ASP.NET Card

Core ASP.NET: This Refcard summarizes the most commonly used core functions and controls in ASP.NET

Try it! Its free but requires registration.