Saturday, March 03, 2007

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

HTML code:
<asp:listbox id="listBox" DataSource="<%# dtCase_Type %>" runat="server" selectionmode="Multiple" datasource="" datavaluefield="Code" datatextfield="Description">

VB code behind:
Private Sub myDataGrid_Itembound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim intRow As Integer = CInt(e.Item.ItemIndex)
If Not IsDBNull(datatableCache.Rows(intRow)("DataFieldFromDataStore")) Then
'datatableCache.Rows(intRow)("DataFieldFromDataStore") contains the list of values (in this case comma seperated)
Dim aryValues() As String = datatableCache.Rows(intRow)("DataFieldFromDataStore").ToString.Split(","c)
Dim listBox As ListBox = e.Item.FindControl("listBox")
For Each li As ListItem In listBox.Items
If Array.IndexOf(aryValues, li.Value) > -1 Then li.Selected = True
Next
End If
End If
End Sub

CDO for receiving emails via SMTP

I recently discovered that .NET 2.0 does not have an inbuilt class for receiving emails via SMTP (or by any other means). Surprising omission. I started to write my own email file parser, but then had a thought... Why not use the old CDO (aka CDOSYS) like the old ClassicASP days? This is how to do it...

Create a reference in Visual Studio to Microsoft CDO for Windows 2000. Now use some code like this:

Dim objSMTP As New System.Net.Mail.SmtpClient
Dim strMailbox As String = objSMTP.PickupDirectoryLocation.Substring(0, objSMTP.PickupDirectoryLocation.LastIndexOf("\")) & "\Mailbox"
Dim CDODropDir As New CDO.DropDirectory
Dim CDOMessages As CDO.IMessages = CDODropDir.GetMessages(strMailbox)
Dim CDOMessage As CDO.Message
For Each CDOMessage In CDOMessages
'Some useful properties:
'CDOMessage.To, CDOMessage.CC, CDOMessage.BCC, CDOMessage.From
'CDOMessage.ReceivedTime, CDOMessage.Subject
'CDOMessage.TextBody, CDOMessage.HTMLBody
'CDOMessages.FileName(CDOMessage) '--Useful for deleting messages later (after you read the whole collection first!)
Next

Friday, November 03, 2006

Web cache gets object references - not a copy

Did you know that if your retrieve an object from the cache, such as a DataView, that the object returned is a reference to the actual object. This means that if you plan to use .RowFilter, then you can end up with concurrency issues if other users are reading from the cache too. The solution is to create a new dataview from a cached table.

Bad:
Dim dvUsers As DataView = CType(_Context.Cache.Get("Users"), DataView)
dvUsers.RowFilter = "User_No=2"

Return dvUsers("Name")

Good:
Dim dtUsers As DataTable = CType(_Context.Cache.Get("Users"), DataTable)
Dim dvUsers As New DataView(dtUsers)
dvUsers.RowFilter = "User_No=2"
Return dvUsers("Name")

Thursday, October 05, 2006

DataFormatString in a BoundField of a GridView

I had a simple little problem - the DataFormatString="{0:0}" in a BoundField was not working. After a bit of fiddling i found that the HTML Encoding was stopping it (by encoding the format string before it was applied). Simply setting HtmlEncode="False" in that BoundField fixed the issue.

Friday, September 29, 2006

Client Validators not working in Firefox & Menu Control with master pages not working

If you upgraded a .NET 1.x project to 2.0 and now have issues with Client Validators not functioning in Firefox (they work server side) or the menu control is displaying the menu items all over the place when used with master pages, then try this tip. In the Web.Config, if there is the tag:

then remove it - it is the root of all evil - unless you plan to use IE 3!
One interesting thing to note - removing it changes all of the client side id's to .NET 2's new way of doing things, so if you have written some javascript into your pages - changes may be required.

blog.start()

I am finally going to get my act into gear and start blogging. I have a few things to share, so i'll get those up in the next week. Then I need to rebuild my web site and move it to a new host. I have a few new things - personal and web dev to add to that too.

Wednesday, January 19, 2005

First Post

Hi, this is my first post. I'll add some text and photos soon.