Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-04-08
3
1,356 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

838 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