从具有母版页的页面调用javascript代码时出现问题. [英] Problem calling javascript code from page having masterpage.

查看:100
本文介绍了从具有母版页的页面调用javascript代码时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我只是遇到一个小问题.我想在我的项目中页面之一的页面加载上检索用户的客户端时间.我已经为它编写了以下代码,但是有些代码没有显示时间.

Hello Friends,

I am just stuck up with a small problem. I want to retrieve the client side time of the user on the page load of one of the page in my project.I have written following code for it.But some how its not showing the time.

function DisplayDateTime()
    {
        var DateTime = new Date();
        var strYear= DateTime.getYear();
        var strMonth= DateTime.getMonth() +1;
        var strDay = DateTime.getDate();
        var strHours = DateTime.getHours();
        var strMinutes = DateTime.getMinutes();
        var strSeconds = DateTime.getSeconds();
        var dateTime=document.getElementById("lblDateTime");
        dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
    }



现在我想在页面加载事件上调用此函数.由于我的页面被母版页吞没,因此无法在加载正文时调用此函数.所以可能我必须从后面的代码中调用它.

谁能告诉我如何做到这一点?

好的,Friendz我在这里找到了一些东西.如果该页面被母版页面吞没,则Java脚本代码不起作用.但是,如果我从没有母版页的简单页面调用它,它就可以正常工作.

谁能告诉我这个问题?

谢谢你

Swapnil Borkar



Now I want to call this function on the page load event.I cannot call this on load of body because my page in engulfed in the master page. So probably I will have to call it from code behind.

So can anyone tell me how to accomplish this?

Ok friendz I have found something here. If thepage is engulfed in a master page then the java script code doesn''t work. But if I call it from a simple page without master page it works fine.

Can anyone tell me about this problem?

Thanking You

Swapnil Borkar

推荐答案

好吧,尝试以下操作:
Well, try something like:
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "DisplayDateTime()", true);



附言:RegisterClientScriptBlock 方法将脚本块添加到呈现页面的顶部.同时,由RegisterStartupScript 方法添加的脚本块在页面完成加载后但在引发页面的OnLoad事件之前执行.
(这意味着RegisterStartupScript方法将脚本块添加到呈现页面的末尾.)



P.S.: The RegisterClientScriptBlock method adds a script block to the top of the rendered page. While, the script block added by the RegisterStartupScript method executes when the page finishes loading but before the page''s OnLoad event is raised.
(it means the RegisterStartupScript method adds a script block to the end of the rendered page.)


好吧,我知道了.

这是因为我在使用

var dateTime = document.getElementById("lblDateTime");

在我的代码中.

由于我访问母版页上的字段,因此我必须写

var dateTime = document.getElementById("ctl00_ContentPlaceHolder1_lblDateTime");

但还有一个问题.

此解决方案在Windows 7上不起作用...:confused:

问题!!!问题!! :((
Ok friendz I got it.

Its because I was using

var dateTime=document.getElementById("lblDateTime");

in my code.

Since I accessing the field on master page I will have to write

var dateTime=document.getElementById("ctl00_ContentPlaceHolder1_lblDateTime");

but there is one more problem.

This solution does not work on Windows 7...:confused:

Problems!!! Problems!! :((


这篇关于从具有母版页的页面调用javascript代码时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆