So I needed to do a little CRUD ListView for a simple app I’m working on. One of the columns is an image path that I was using an Image object to display. The object got vetoed and I’m going with text now so I wanted a clean way to either show a simple text link to pop the image on a new tab or show that no image was available. I went with this in the template:
<td><%# GenerateImageField(Eval("ImagePath")) %> </td>
public string GenerateImageField(object dataItem){
if (dataItem == null || string.IsNullOrEmpty(dataItem.ToString()))
{
return "No Image";
}
return @"<a href='"+ dataItem.ToString() +@"' target='_blank'>Image</a>";
}
This works out rather well.
Maybe I’m just lucky but I’ve only had to use control tags in ASP.Net apps very infrequently and I always forget how they work until I need them again. Sherlock Holmes would say thats a good thing but I’m going to post a little example here for my own benefit.
I LOVE the ListView control that we got in 3.5 and I’m using it alot in the dinky apps I’m banging out of late. For this one I needed to create a little link in the grid for each row using the current querystring and the row’s ID.
<
asp:ListView ID=”lvLicenses” runat=”server” DataSourceID=”dsEmployeeLicense”>
<ItemTemplate>
<tr style=”background-color: #E0FFFF;color: #333333;“>
<td>
<a href=’EmployeeLicense.aspx?EmpID=<%=Request.QueryString["ID"]%>&ID=<%# Eval(“ID”) %>‘ target=”_self”>Edit</a>
</td>
Easy as pie. mmm pie.
So this was a little harder than I thought it would be. I’m still a little shocked that it wasn’t a more common question. So here is what we’re going for, a ListView databound to a LinqDatasource which has a column with an image path. We want to show that image in the grid and allow for uploading new images to new and existing rows. Like this:

And the insert

The source for the above would look like this:

See that we’re just wrapping the image in a link and shrinking it down.

Again, no biggie, just a regular FileUpload control.
Now lets look at the real magic thats going to make this work. The first thing we need is a way to find the control from our code-behind. This is pilfered from everyone’s friend Jeff Atwood from over at Coding Horror.

Then we’re going to going to override the LINQ inserting and inserted events:

This should be pretty straightforward. We grab a handle to the FileUpload object and create the server path (I know, it should be inside the IF but I don’t want to redo the screenshot) Then we save the file to the server.

Now after the row has been inserted we’ll check that FileUpload control again, get the row that was just inserted, set the path and update the row just in time for the postback.

You could probably wrap the whole thing up in an UpdatePanel and make it all slick and Ajaxy but I think what we’ve got here is pretty neat as is. Enjoy.
So we wrap everything in regions. I know some coders scoff at that but we think it keeps the code tidy. One of the downsides is that there isn’t a keyboard shortcut to collapse all regions without collapsing everything else too. One of my wonderful co-workers found one.
Here is what you need to do. Go to Tools/Macros/Macro IDE in Visual Studio.

Create a new macro and name it RegionTools. (This is important as it has to match the module name.
Drop the following code into it.
Imports EnvDTE
Imports System.Diagnostics
Public Module RegionTools
Sub ExpandAllRegions()
Dim objSelection As TextSelection
DTE.SuppressUI = True
objSelection = DTE.ActiveDocument.Selection()
objSelection.StartOfDocument()
Do While objSelection.FindText(“#region”, vsFindOptions.vsFindOptionsMatchInHiddenText)
Loop
objSelection.StartOfDocument()
DTE.SuppressUI = False
objSelection = Nothing
End Sub
Sub CollapseAllRegions()
Dim objSelection As TextSelection
ExpandAllRegions()
DTE.SuppressUI = True
objSelection = DTE.ActiveDocument.Selection()
objSelection.EndOfDocument()
Do While (objSelection.FindText(“#region”, vsFindOptions.vsFindOptionsBackwards))
DTE.ExecuteCommand(“Edit.ToggleOutliningExpansion”)
objSelection.EndOfDocument()
Loop
objSelection.StartOfDocument()
DTE.SuppressUI = False
objSelection = Nothing
End Sub
End Module
That done you can map the macro to a proper shortcut as I’ve done using Ctl-Shift–

Enjoy.
Sometimes we can use Active Directory for our application security. There are a number of ways to attack the problem including the oft painful LDAP. If we need to check to see if the current user is a member of an AD group we can do it thus:
// We can start by making sure that our current principal is set correctly as it probably isn’t.
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
// Then we can save some time with an obvious check
if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
{
// Now we have only to specify the name of the group in AD. Here I have
// the name “XXX_Administrators” refrenced from the resource file.
if (Thread.CurrentPrincipal.IsInRole(Properties.Resources.AdminRole))
{
return SecurityGroup.Administrators;
}
