Friday, 25 May 2007

ASP.NET - Gridview HTMLEncode

Ever been frustrated that when you Auto generate columns, you cannot set the HTMLEncode property to false so that it renders HTML inside the Gridview?

Simple place the code inside the Gridviews RowDataBound event:



If (e.Row.RowType = DataControlRowType.DataRow) Then

Dim cells As TableCellCollection
cells = e.Row.Cells
Dim cell = New TableCell

For Each cell In cells
cell.Text = Server.HtmlDecode(cell.Text)
Next

End If



Now every cell that has HTML inside will get rendered, such as HREF's etc ;)

Wednesday, 23 May 2007

ASP.NET/Java/ASP - Run External File

This is code I use in an Intranet to execute a file on a server. Just place it in the Head of the page:




SCRIPT Language="JScript"

function openOutlook() {
myfile="R:\\Shortcuts\\Outlook.lnk";
WSH=new ActiveXObject("WScript.Shell");
WSH.Run(myfile)
}

/SCRIPT



Remember to add the < and />'s!

Now simply call a href with javascript:openOutlook and voila! Also note that the path MUST have \\ and NOT a single \

ASP.NET - web.config and posting attachments

Ever wondered why you can only post requests around 4mb in your ASP.NET pages?

Simply edit the web.config file and add the following under the system.web section:



httpRuntime maxRequestLength="100000"


Remember to add < at the start and /> at the end of the line (this blog posting will not render the line properly if I type it!)

You can now post files (for example, into a database) larger than you ever would want to.

ASP.NET - File Information

Want to know the subject/author/created date/etc... of a file in ASP.NET? Well you can use a slightly adapted version of my Classic ASP example. Note that the version below is for a server running on Windows 2000.



Function FileInfo(ByVal filename, ByVal Attribute)

Dim shortname
shortname = Right(filename, Len(filename) - InStrRev(filename, "\"))
Dim objShell = CreateObject("Shell.Application")
Dim objFolder = objShell.Namespace(Left(filename, InStrRev(filename, "\")))
Dim folderitem = objFolder.parsename(shortname)
Dim detail = ""

'// Windows 2000 attributes
'// =======================
'// Name, Size, Type, Modified, Attributes, Comments
'// Created, Accessed, Owner, Author, Title, Subject
'// Category, Pages, Copyright, CompanyName, ModuleDescription
'// ModuleVersion, ProductName, ProductVersion, SenderName
'// RecipientName, RecipientNumber, CSID, TSID, TransmissionTime
'// CallerID, Routing, AudioFormat, SampleRate, AudioSampleSize
'// AudioChannels, PlayLength, FrameCount, FrameRate
'// VideoSampleSize, VideoCompression

Select Case Attribute
Case "Size"
detail = objFolder.GetDetailsOf(folderitem, 1)
Case "Type"
detail = objFolder.GetDetailsOf(folderitem, 2)
Case "Modified"
detail = objFolder.GetDetailsOf(folderitem, 3)
Case "Attributes"
detail = objFolder.GetDetailsOf(folderitem, 4)
Case "Comments"
detail = objFolder.GetDetailsOf(folderitem, 5)
Case "Created"
detail = objFolder.GetDetailsOf(folderitem, 6)
Case "Accessed"
detail = objFolder.GetDetailsOf(folderitem, 7)
Case "Owner"
detail = objFolder.GetDetailsOf(folderitem, 8)
Case "Author"
detail = objFolder.GetDetailsOf(folderitem, 10)
Case "Title"
detail = objFolder.GetDetailsOf(folderitem, 11)
Case "Subject"
detail = objFolder.GetDetailsOf(folderitem, 12)
Case "Category"
detail = objFolder.GetDetailsOf(folderitem, 13)
Case "Pages"
detail = objFolder.GetDetailsOf(folderitem, 14)
Case "Copyright"
detail = objFolder.GetDetailsOf(folderitem, 15)
Case "CompanyName"
detail = objFolder.GetDetailsOf(folderitem, 16)
Case "ModuleDescription"
detail = objFolder.GetDetailsOf(folderitem, 17)
Case "ModuleVersion"
detail = objFolder.GetDetailsOf(folderitem, 18)
Case "ProductName"
detail = objFolder.GetDetailsOf(folderitem, 19)
Case "Version"
detail = objFolder.GetDetailsOf(folderitem, 20)
Case "SenderName"
detail = objFolder.GetDetailsOf(folderitem, 21)
Case "RecipientName"
detail = objFolder.GetDetailsOf(folderitem, 22)
Case "RecipientNumber"
detail = objFolder.GetDetailsOf(folderitem, 23)
Case "CSID"
detail = objFolder.GetDetailsOf(folderitem, 24)
Case "TSID"
detail = objFolder.GetDetailsOf(folderitem, 25)
Case "TransmissionTime"
detail = objFolder.GetDetailsOf(folderitem, 26)
Case "CallerID"
detail = objFolder.GetDetailsOf(folderitem, 27)
Case "Routing"
detail = objFolder.GetDetailsOf(folderitem, 28)
Case "AudioFormat"
detail = objFolder.GetDetailsOf(folderitem, 29)
Case "SampleRate"
detail = objFolder.GetDetailsOf(folderitem, 30)
Case "AudioSampleSize"
detail = objFolder.GetDetailsOf(folderitem, 31)
Case "AudioChannels"
detail = objFolder.GetDetailsOf(folderitem, 32)
Case "PlayLength"
detail = objFolder.GetDetailsOf(folderitem, 33)
Case "FrameCount"
detail = objFolder.GetDetailsOf(folderitem, 34)
Case "FrameRate"
detail = objFolder.GetDetailsOf(folderitem, 35)
Case "VideoSampleSize"
detail = objFolder.GetDetailsOf(folderitem, 36)
Case "VideoCompression"
detail = objFolder.GetDetailsOf(folderitem, 37)
End Select
Return (detail)

End Function


Use by calling:



FileInfo("C:\file.txt","Subject")

Thursday, 10 May 2007

VB.NET - Give me dates!

Wish to know the date range of last week or next week? What about the current or last months start and end date? Here's how...


Dim startweek
Dim endweek
Dim lastweekstart
Dim lastweekend
Dim currentmonthstart
Dim currentmonthend
Dim lastmonthstart
Dim lastmonthend

Dim today
today = Date.Today

endweek = DateAdd(DateInterval.Day, 1, DateAdd(DateInterval.Day, 7 - DatePart(DateInterval.Weekday, today), today))

startweek = DateAdd(DateInterval.Day, -6, endweek)

lastweekend = DateAdd(DateInterval.Day, -1, startweek)

lastweekstart = DateAdd(DateInterval.Day, -6, lastweekend)

currentmonthstart = DateAdd(DateInterval.Day, -(DatePart(DateInterval.Day, today) - 1), today)

currentmonthend = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, currentmonthstart))

lastmonthstart = DateAdd(DateInterval.Month, -1, currentmonthstart)

lastmonthend = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, lastmonthstart))