Solved

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

Posted on 2009-04-08
3
1,343 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:mathieu_cupryk
  • 2
3 Comments
 

Author Comment

by:mathieu_cupryk
ID: 24108096
Request Attention: I need the question answered asap.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 24111184
what happens when you try this
Page.ClientScript.RegisterStartupScript(this.GetType(), string.Concat("UpdateClock", DateTime.Now.Ticks), "updateClock();", true);
0
 
LVL 16

Accepted Solution

by:
burakiewicz earned 500 total points
ID: 24111205
you said you have ajax so you might need something like this then
ScriptManager.RegisterStartupScript(this, this.GetType(),  string.Concat("UpdateClock", DateTime.Now.Ticks), "updateClock();", true);

or if you have update panels on the page replace UpdatePanel1 with the name of your pnael
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(),  string.Concat("UpdateClock", DateTime.Now.Ticks), "updateClock();", true);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now