Tuesday, September 18, 2012

Making a GridView render the header in the thead tag

By default a ASP.Net GridView control renders everything inside a <tbody> tag, which isn't good when doing Bootstrap tables.  Adding the following will force it to render inside the <thead> and <tfoot> tags.

            myGridView.DataBound += (source, args) =>

            {
                var gridView = (GridView)source;

                if (gridView.HeaderRow != null)
                {
                    gridView.HeaderRow.TableSection = TableRowSection.TableHeader;
                }

                if (gridView.FooterRow != null && gridView.ShowFooter)
                {
                    gridView.FooterRow.TableSection = TableRowSection.TableFooter;
                }
            };


Source:
http://www.west-wind.com/weblog/posts/2010/Nov/18/Adding-proper-THEAD-sections-to-a-GridView

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

I've encountered this error a few times with my own custom controls and with the AjaxToolkit when I was upgrading a legacy project with a UI refresh.

In both cases, I had added a .js reference in my <head> tag that resolved using <%= Page.ResolveUrl("my.js") %>.  Apparently .Net doesn't like that when it tries to add a another header dynamically from a control (i.e. registering a new WebResource .css file).

Example:

    <head id="headMain" runat="server">
        <title></title>
        <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="~/Content/bootstrap-responsive.min.css" />
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" >
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="<%= Page.ResolveClientUrl("~/Scripts/jquery-1.8.1.min.js") %>"> </script>
        <script type="text/javascript" src="<%= Page.ResolveClientUrl("~/Scripts/bootstrap.min.js") %>"> </script>
    </head>

Fix the problem by changing it to:

    <head id="headMain" runat="server">
        <title></title>
        <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="~/Content/bootstrap-responsive.min.css" />
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" >
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
        <script type="text/javascript" src="<%= Page.ResolveClientUrl("~/Scripts/jquery-1.8.1.min.js"%>"> </script>
        <script type="text/javascript" src="<%= Page.ResolveClientUrl("~/Scripts/bootstrap.min.js"%>"> </script>



Tuesday, August 28, 2012

Force IE to render in non-compatibility mode

Many of our users have to keep compatibility mode for our domain due to SharePoint.

So, I have a lot of trouble getting IE compatibility mode to render Twitter Boostrap the same way it renders on IE8+ in non-compatibility mode.

In comes this meta tag to fix it all:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 



Thursday, September 22, 2011

Replace .Net Validation Summary with jQuery UI dialog

This will replace the older looking .Net Validation Summary built-in JavaScript pop-up with a more modern looking jQuery UI dialog.  It works by hiding the real Validation Summary text, then shows it via a dialog by using a wrapper panel.

A new click event (in addition to your original one) is assigned to the submission button.  When the button is clicked, if .Net sets the Page_IsValid to false, then the dialog is triggered.  Otherwise the page submits like normal.

.Net Code

    <asp:Panel ID="panError" runat="server" CssClass="ui-state-error" Style="displaynone">
        <span class="ui-icon ui-icon-circle-close" style="margin0 7px 50px 0floatleft;">
        </span><span>
            <asp:ValidationSummary ID="summaryMain" EnableClientScript="true" ShowMessageBox="false"
                ShowSummary="true" runat="server" CssClass="ui-state-error-text" style="text-align:left" />
        </span>
    </asp:Panel>

JavaScript (jQuery) Code
    <script type="text/javascript">
        jQuery(document).ready(function () {
            $("#<%= this.btnSave.ClientID %>").click(function () {
                if (!Page_IsValid) {
                    $("#<%= this.panError.ClientID %>").dialog({
                        resizable: false,
                        width: 420,
                        modal: true,
                        buttons: {
                            'Close'function () {
                                $(this).dialog('close');
                            }
                        }
                    });
                }
            });
        });
    </script>

Friday, September 9, 2011

Using Linked Files in a Web Application Project

I was in need of a way to share JavaScript files, CSS, and other static files (i.e. non-compiled) between web application projects.  I continually maintained and updated the files on the main web project, but rarely kept up with the secondary project until it was absolutely necessary. 

This great find lets me use the Linked files feature of Visual Studio with Web Application projects.  By default the linked files are only copied to the /bin directory and not to the actual web application project structure for actually viewing the website.

http://consultingblogs.emc.com/jamesdawson/archive/2008/06/03/using-linked-files-with-web-application-projects.aspx

Monday, August 15, 2011

Unknown server tag 'asp:ScriptManager'

LiveJournal Tags:
This exception has popped up more than once for me, but the answer when searching for help is always the same.  Add the following to your web.config (or similar based on your version of .Net).
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
This simple solution of course is never the case for me.  If you have the above already in your web.config, the gist of the real problem is that your web.config is actually not loading into the current context due to some other error.
I thought I’d offer some steps I’ve used that seem to always fix the problem for me. 
LiveJournal Tags:
  1. If you are using Local IIS Server as your hosting server, switch to the built-in Visual Studio Development Server temporarily and see if the website loads normally.
    • If your website loads normally in the built-in web server;  sometimes Visual Studio botches the creation of the IIS virtual directory and application.  Check IIS admin to see if your settings are correct (or if the virtual directory even exists!).
  2. Try commenting out the entire script tag (<asp:ScriptManager ID="ScriptManager1" runat="server" />) and see if it reveals the real error.  Any other custom controls on the page may fail as well, so keep commenting out until you either see a different error or the page loads normally.
  3. Add the namespace mapping directly to the page.  This is a last resort, but sometimes necessary to just get by the problem.
    • <@ Register Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" TagPrefix="asp" %>



Monday, August 1, 2011

Manual Adobe Acrobat Reader Download

I get tired of searching for this every time I need it. 

You can manually download any version of Acrobat Reader at:

ftp://ftp.adobe.com/pub/adobe/reader/