Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

need to use page.clientscript.registerstartupscript. in the code behind of the ascx page. onload

I have this in my base class


// this keeps the form action correct during ajax postbacks
            string formActionScript = "<script type=\"text/javascript\">"
                + "Sys.Application.add_load(function() { var form = Sys.WebForms.PageRequestManager.getInstance()._form; form._initialAction = form.action = window.location.href; }); "
                + "</script>";

            Page.ClientScript.RegisterStartupScript(this.GetType(), "formactionset", formActionScript);


I want to call this javascript in the codebehind of the ascx page.
See attached ascx page.

<script type="text/javascript">
<!--

function updateClock ( )
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

// -->
</script>
http://www.elated.com/articles/creating-a-javascript-clock/
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uxLogin.ascx.cs" Inherits="OmegaLove.Web.UI.uxLogin" %>
<script type="text/javascript">
<!--
 
    function UpdateClock() {
        var currentTime = new Date();
 
        var currentHours = currentTime.getHours();
        var currentMinutes = currentTime.getMinutes();
        var currentSeconds = currentTime.getSeconds();
 
        // Pad the minutes and seconds with leading zeros, if required
        currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
        currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
 
        // Choose either "AM" or "PM" as appropriate
        var timeOfDay = (currentHours < 12) ? "AM" : "PM";
 
        // Convert the hours component to 12-hour format if needed
        currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
 
        // Convert an hours component of "0" to "12"
        currentHours = (currentHours == 0) ? 12 : currentHours;
 
        // Compose the string for display
        var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
 
        // Update the time display
        document.getElementById("clock").firstChild.nodeValue = currentTimeString;
    }
 
// -->
</script>
 
<div id="loginbox">
             <asp:LoginView ID="LoginView1" runat="server">
             <AnonymousTemplate>
               <asp:Login ID="LoginStatus" runat="server" Height="31px" width="100%" FailureAction="RedirectToLoginPage"
                    onloggedin="LoginStatus_LoggedIn" onloggingin="LoginStatus_LoggingIn">
                  <LayoutTemplate>
                   <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                           <td nowrap="nowrap" width="25%">
                           <asp:Label runat="server" ID="lblUserName" AssociatedControlID="UserName" Text="Username:"  />
                           <asp:TextBox id="UserName" runat="server" BorderColor="DarkGray"  
                                   BorderStyle="Inset" BorderWidth="2px" Width="125px" />
                           </td>
                            
                           <td width="8px" style="text-align: left;" valign="middle">
                           <asp:RequiredFieldValidator ID="valRequireUserName" runat="server" SetFocusOnError="True"
                                 ControlToValidate="UserName" Text="*" ValidationGroup="Login"  
                                   Font-Bold="True"  />                     
                           </td>
                          
                          <td nowrap="nowrap" width="25%">
                          <asp:Label ID="lblPassword" runat="server" AssociatedControlID="Password" Text="Password:" />
                          <asp:TextBox ID="Password" runat="server" TextMode="Password"
                                   BorderColor="DarkGray" BorderStyle="Inset" BorderWidth="2px" 
                                  Width="125px" />
                          </td>
                          
                          <td width="8px" style="text-align: left;" valign="middle">
                                <asp:RequiredFieldValidator ID="valRequirePassword" runat="server" 
                                    ControlToValidate="Password" SetFocusOnError="True" Text="*" 
                                    ValidationGroup="Login" Font-Bold="True" />
                          </td>
                          
                          <td width="25%">
                               <asp:Button CssClass="button-login" validationgroup="Login" 
                                   CommandName="Login" ID="btnLogin" 
                                   runat="server" Text="Login" Font-Bold="True" />
                                    
                          </td>
                              
                                        
                        </tr>
                        
                        <tr>
                            <td width="100%" colspan="5" style="text-align: right;">
                                <asp:HyperLink ID="lnkRegister" runat="server" NavigateUrl="~/Secure/Register.aspx">Create New Account 
                                </asp:HyperLink>
                                | <asp:HyperLink ID="lnkPasswordRecovery" runat="server" 
                                    NavigateUrl="~/Secure/PasswordRecovery.aspx">Forgot 
                                password?</asp:HyperLink>
                            </td>
                        </tr>
 
                        <tr>
                            <td width="100%" colspan="5" style="text-align: right;">
                               <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                               <input type="text" class="value" name="currentTimeString" size="27" readonly="readonly" style="border: 0px;">
                            </td>
                        </tr>
                     </table>
                    </LayoutTemplate>
               </asp:Login>
              </AnonymousTemplate>
                
            <LoggedInTemplate>
               <div id="welcomebox">
                  <asp:LoginName ID="LoginName1" runat="server" FormatString="Welcome {0}" />
                  <asp:Button CssClass="button-login" ID="btnLogout" runat="server" Text="Logout" Font-Bold="True" 
                   OnClick="btnLogout_Click" /><br />
                   <input type="text" class="value" name="currentTimeString" size="27" readonly="readonly" style="border: 0px;">
              </div>
            </LoggedInTemplate>
         </asp:LoginView>
    
<script language="JavaScript" type="text/javascript">
      UpdateClock();  
</script>                   
                
        </div>

Open in new window

Avatar of mathieu_cupryk
mathieu_cupryk
Flag of Canada image

ASKER

Request Attention: I need the question answered asap.
what happens when you try this
Page.ClientScript.RegisterStartupScript(this.GetType(), string.Concat("UpdateClock", DateTime.Now.Ticks), "updateClock();", true);
ASKER CERTIFIED SOLUTION
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial