tags:

views:

10

answers:

0

I need to modify aspx page's html to insert few scripts after and tags. I have implemented this by iterating through page's control collection and modifying LiteralControl text. As ASP.NET compiles all HTML elements and readable text that do not require server-side processing into instances of LiteralControl class. Hence the page will have 3 child controls, two LiteralControls and one HtmlForm control.

This is working fine for all the pages in the application except one. For this aspx page, the page's controls collection's first element is not a literal with . In fact controls collection contains only one control, HtmlForm control.

I will appreciate if anyone can help me figure out why this page's controls collection's first element is not a literal with .

Hi Spinon, here is C# code behind and html code sample.

//Insert the script and body attributes to the page html. Ctrl = this;

        foreach (Control InnerCtrl in Ctrl.Controls)
        {
            if (InnerCtrl is LiteralControl)
            {
                string literalControlText = ((LiteralControl)InnerCtrl).Text;
                int inx = literalControlText.ToLower().IndexOf("</head>");
                if (inx > 0)
                {                        
                    literalControlText = literalControlText.Insert(inx + 8, HeadWrtr);  //after </head> and before <body>
                }
                int bodyIndex = literalControlText.ToLower().IndexOf("<body");
                if (bodyIndex > -1)
                {                    
                    literalControlText = literalControlText.Replace("<body", BodyAttributes);
                }                    

                ((LiteralControl)InnerCtrl).Text = literalControlText;
            }
        }

<%@ Register TagPrefix="bar" TagName="NavigationBar" Src="NavigationBar.ascx" %> <%@ Register TagPrefix="uc1" TagName="TabNavigator" Src="TabNavigator.ascx" %> <%@ Page language="c#" Codebehind="MyWebPage.aspx.cs" AutoEventWireup="True" Inherits="TestWeb.MyWebPage" %> <%@ Register TagPrefix="mweb" Namespace="Test.Web.Core.UI.Controls" Assembly="Test.Web.Core" %> <%@ Register TagPrefix="UIRenderers" Namespace="TestWeb.UI.Controls" Assembly="Test.Renderers" %> <%@ Register TagPrefix="stockScreen" TagName="StockScreenerNavigator" Src="StockScreenerNavigator.ascx" %> Test

function DisplayMetricDef(ric, id) { var temp = id.split(":"); var taxo = temp[0]; var tag = temp[1]; LoadPopupWithParams('Definition.aspx?Tag=' + tag + '&Taxo=' + taxo + '&RIC=' + ric +'&<%=GuidHiddenFieldName%>=<%=CompsParamsCacheKey%>', 900, 600, 'yes', 'yes', 'yes', 'metricDefinition'); return false; }

        function ConfirmSubmit(type_)
        {
            return window.confirm('Your current ' + type_ + ' has not been saved and will be lost. Do you wish to continue?'); 
        }                       
    </script>
</head>           
<body>      
    <form id="Form1" method="post" runat="server">
        <div id="metricName" style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 10px; Z-INDEX: 5000; LEFT: 10px; VISIBILITY: hidden; PADDING-BOTTOM: 4px; BORDER-LEFT: gray 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: gray 1px solid; FONT-FAMILY: verdana; POSITION: absolute; TOP: 10px; BACKGROUND-COLOR: #ffffcc"
            name="metricName"></div>
        <iframe id="divShim" style="DISPLAY: none; LEFT: 0px; WIDTH: 300px; POSITION: absolute; TOP: 0px; HEIGHT: 64px"
            src="javascript:false;" frameBorder="0" scrolling="no"></iframe>
        <table cellSpacing="0" cellPadding="0" width="100%" border="0">
            <tr>
                <td><MWEB:BANNER id="Banner2" runat="server" SharedAppPath="\path1\dir\ssShared"></MWEB:BANNER></td>
            </tr>               
        </table>
        <table style="MARGIN-TOP: 0px; MARGIN-LEFT: 10px" cellSpacing="0" cellPadding="0" width="905"
            border="0">
            <tr>
                <td><bar:navigationbar id="TabsBar" runat="server" Visible="False" ShowResultsButtonsOnNextRow="False"
                        ShowResultImageButtons="True" BarAlign="left" BarType="tab" PageName="Results" EnableViewState="False"></bar:navigationbar><uc1:tabnavigator id="TabNavigator1" runat="server" SelectedTab="web"></uc1:tabnavigator></td>
                <td vAlign="bottom" noWrap align="center"><asp:label id="ResultTimestamp" runat="server" EnableViewState="False" Visible=false></asp:label></td>
            </tr>
        </table>
        <table id="firstTable" style="MARGIN-TOP: 5px; MARGIN-LEFT: 10px" cellSpacing="0" cellPadding="0"
            border="0">
            <tr>
                <TD><IMG src="images/topleftcurve.gif"></TD>
                <TD><IMG height="5" src="images/topborder.gif" width="100%"></TD>
                <TD><IMG src="images/toprightcurve.gif"></TD>
            </tr>
            <tr>
                <TD><IMG height="33" src="images/leftborder.gif" width="5"></TD>
                <TD>
                    <TABLE class="bgGreyLighter" cellSpacing="2" cellPadding="2" border="0">
                        <TR>
                            <td style="BORDER-RIGHT: #cccccc 2px solid" align="left"><asp:hyperlink id="CriteriaLink" EnableViewState="False" Font-Bold="True" Runat="server" NavigateUrl="javascript:void(0);">{0} Criteria...</asp:hyperlink>&nbsp;</td>
                            <td><b>Save and Set Alerts:</b>
                            </td>
                            <td style="BORDER-RIGHT: #cccccc 2px solid" align="left"><asp:imagebutton id="SaveCompsButton" runat="server" EnableViewState="False" ImageUrl="images\icon-save.gif"></asp:imagebutton></td>                               
                            <td align="left"><MWEB:APPNAVIGATOR id="AppNavigator1" runat="server" SharedAppPath="\path1\dir\ssShared"></MWEB:APPNAVIGATOR></td>
                        </TR>
                    </TABLE>
                </TD>
                <TD><IMG height="33" src="images/rightborder.gif" width="5"></TD>
            </tr>
            <tr>
                <TD><IMG src="images/bottomleftcurve.gif"></TD>
                <TD><IMG height="5" src="images/bottomborder.gif" width="100%"></TD>
                <TD><IMG src="images/bottomrightcurve.gif"></TD>
            </tr>
        </table>            
        <table id="maintable3" style="MARGIN-TOP: 2px; MARGIN-LEFT: 10px" cellSpacing="0" cellPadding="0"
            border="0">
            <tr>
                <td><UIRENDERERS:RESULTSCONTROL id="ResultsHolder" runat="server"></UIRENDERERS:RESULTSCONTROL></td>
            </tr>
        </table>            
    </form>
</body>

related questions