How to install IIS on Windows XP without CD

•November 19, 2007 • 127 Comments

Today I was at the University and wanted to install IIS and ASP.NET to work on a small project. The only problem is that I didn’t have the Windows XP CD with me. Google returned answers that involved downloading the SP2 installer and extracting the files from it. So, I decided to hack around and see what I could come up with without having to download the ~250Mb SP2 installer. Here’s what I found.

Open up Add Remove Programs and click on the Windows Components button. In here, check off the IIS service and click Next. It will then prompt you for the Windows CD. When it does, click OK (even though we haven’t put one in). In the next window browse to C:WindowsI386 and click OK. It should then start installing. During the installation it may prompt for the CD again. Rinse and repeat the exact same process by locating the I386 folder and it should install just fine.

Cheers!

Printing To A Local Or Network Printer

•November 13, 2007 • 9 Comments

Printing any information on a website has mostly been done by using the Javascript “window.print()” function. But what if you want to, for example, print to a label printer, or some other resource that requires specific formatting for the print job (and your CSS markup just doesn’t cut it)?

Well, with ASP.NET 2.0 we can accomplish such a feat using the System.Drawing.Printing namespace. Normally this namespace is popular for Forms based applications, but it can also be implemented into a web application to allow for greater customization and extension when printing.

The first thing to do is to import the System.Drawing.Printing namespace. This gives us direct access to the methods for controlling our printing. We also want to create a new PrintDocument object. This should be a privately declared object that would go above your methods / properties in your code-behind file. In VB.NET we must also declare the variable as WithEvents so we can have access to it’s methods. We would do this like so:

Private WithEvents myPrinter As New Printing.PrintDocument()

Next, we add a control that will fire the printing. For this example, we’ll use a button and give it an ID of “ButtonPrint”. This button can be placed from the designer view of the page. Then in the code behind file, create a method that will handle the button’s click event. In this sub routine we also need to set the printer name we want to use. For this example, the emphasis is on network printers (since so many people have trouble printing to them), so below is an example of the name of a network printer. Then, we call the print method to start the printing.

Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _ ButtonPrint.Click
      myPrinter.PrinterSettings.PrinterName = “serverNameepson
      myPrinter.Print()
End Sub

Next we want to handle the myPrinter.PrintPage() event. Inside of this method, we have to draw our string onto the page. Here is an example of printing the current date and time.

Private Sub myPrinter_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles myPrinter.PrintPage

Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim printFont = New Font(“Arial”, 10)
Dim sb As StringBuilder = New StringBuilder()

sb.Append(“DateTime: “ + DateTime.Now.ToString() + Environment.NewLine)
e.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, topMargin
)

End Sub

This is probably the hardest section because of the e.Graphics.DrawString() method etc. Here’s what the parameters represent:

1. The string to draw.
2. The font to use.
3. The color of the string.
4. The x position to draw the string (in this case we’ve grabbed the margins and are drawing from there).
5. The y position to draw the string (same as the x position).

You’re going to have to play around with those things, and drawing multiple strings / shapes / images to get the hand of things.

One question that I had when I first learned this is what if I have multiple pages? What if I’m printing some huge table of values? Thankfully, the event has a hasMorePages() flag that can be raised when, for example, the next string you draw will exceed the height of the page. In this case, you’ll want some sort of global variable that will keep track of the index in your loop.

Printing can be a very cool feature to implement, but it can become extremely tricky to implement when you have more than one page. Hopefully this tutorial has shed some light of the basics of printing with ASP.NET and perhaps in the future I will go into more detail about multiple page printing.

Validators and Page Cancellation

•November 13, 2007 • Leave a Comment

While working on a current project with ASP.NET 2.0, I needed to add some validators to a form. Thankfully, ASP.NET has built in range, required and custom (where you write your own Javascript validator) validators. They work really well by including some already made Javascript functions to your page. When you click your submit button for your form, the Javascript validators run and check any fields that need validating.

But wait, what happens if you add another button, or for that matter, any control that performs a post back? Well they all try and validate the form. What if (like in my case) you only want a specific control to “activate” these validators (ie. your “submit” button). Well, here’s the solution for the problem.

For each validator that you create, you must give it a ValidationGroup that it belongs to. Here’s an example of that:

<asp:TextBox runat="server" ID="TextBox1" /> <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="This field is required!" SetFocusOnError="true" ValidationGroup="MyRequiredFields" />

Once we’ve added the ValidationGroup to the validator, we must add the same ValidationGroup to the control that we wish to submit the form.

<asp:LinkButton runat="server" ID="LinkButtonSave" Text="Save" ValidationGroup="MyRequiredFields" />
<asp:LinkButton runat="server" ID="LinkButtonCancel" Text="Cancel" OnClick="CancelAsset" />

Now we can attach any method to the “cancel” button or any other control and it won’t interfere with the validation of our form.

Cheers!