Archive

Archive for September, 2006

Creating Sites in IIS 6.0 And Automatically Switching Them to use the 2.0 Framework in c#

September 18th, 2006 Michael Bell No comments

So, you’ve got the code in place to create a site in IIS via c# in one of your web apps, but you want the site ASP.NET version to default to 2.0. I had the problem and couldn’t find a simple “switch” to flip, so I wound up writing this little piece of code that goes in and manually remaps the files that need it, from the 1.1 framework to the 2.0. After that runs, your IIS ASP.NET version will read 2.0, and act accordingly :)

ManagementObjectSearcher searcher = new ManagementObjectSearcher(@”root\MicrosoftIISv2″,
    “SELECT * FROM IIsWebVirtualDirSetting WHERE Path = ‘” + pathToRoot.Replace(@”\”, @”\\”) + webSiteName + “‘”);

foreach (ManagementObject queryObj in searcher.Get())
{
    if (queryObj.Properties["ScriptMaps"] != null)
    {
        Object[] arrScriptMaps = (Object[])(queryObj["ScriptMaps"]);
        foreach (Object arrValue in arrScriptMaps)
        {
            foreach (PropertyData data in ((ManagementBaseObject)arrValue).Properties)
            {
                if (Convert.ToString(data.Value).ToLower() == @”c:\windows\microsoft.net\framework\v1.1.4322\aspnet_isapi.dll”)
                    if (data.Name == “ScriptProcessor”)
                        data.Value = @”c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll”;
            }
        }
        queryObj.SetPropertyValue(”ScriptMaps”, arrScriptMaps);
        queryObj.Put();
    }
}

Categories: ASP.NET, Whatever, c# Tags:

Binding to an ObjectDataSource where the Object has Child Objects

September 6th, 2006 Michael Bell No comments

I was trying to bind a DetailsView to an ObjectDataSource and display child class properties of the main Object I was binding to. I had a BillingObject with a child object called AddressObject.

My failed attempt:
<asp:BoundField DataField=”BillingAddress.State” HeaderText=”State” SortExpression=”BillingAddress.State” />

I did a little research and found out the idea is very possible. In case anyone else comes across this, you have to use an ItemTemplate:
<asp:TemplateField HeaderText=”First Name” SortExpression=”Person.FirstName”>
    <ItemTemplate>
        <asp:Label ID=”FirstNameLabel” runat=”server” Text=’<%# Eval(”Person.FirstName”) %>’></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Categories: ASP.NET, c# Tags:

Problems with DataFormatString not workin in 2.0 controls?

September 1st, 2006 Michael Bell No comments

It didn’t take long for me to run into this once I started playing with controls in 2005. In 2.0, if you want the following field to be displayed in currency, you would change it from this:
<asp:BoundField DataField=”Price” HeaderText=”Price” SortExpression=”Price” />
to this:
<asp:BoundField DataField=”Price” DataFormatString=”{0:c}” HeaderText=”Price” SortExpression=”Price” />

BUT, once you do this, you will find that yoru DataFormatString command had no effect whatsoever on your displayed values. To get this to work correctly, you have to also include “HtmlEncode=’false’” in your control definition. The finished product looks like this:
<asp:BoundField DataField=”Price” HtmlEncode=”false” DataFormatString=”{0:c}” HeaderText=”Price” SortExpression=”Price” />

Categories: ASP.NET, c# Tags: