Solved

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

Posted on 2009-04-08
3
1,339 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
Comment Utility
Request Attention: I need the question answered asap.
0
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contant UI messages in .NET 1 40
Problem to Popup 37 78
Async and await with MVC and Web Api 3 29
Duplicate a row 2 29
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

12 Experts available now in Live!

Get 1:1 Help Now