简体   繁体   English

如何在子页面的onload事件中调用JavaScript

[英]How to call javascript in a child page's onload event

I have an ASP.net application with an aspx page. 我有一个带aspx页面的ASP.net应用程序。

In Page_Load event of the aspx page, I'm handling some code which is based on the value of a hidden variable from the javascript(assigning result of javascript to hidden variable). 在aspx页面的Page_Load事件中,我正在处理一些代码,这些代码基于javascript中隐藏变量的值(将javascript的结果分配给隐藏变量)。

I am calling the javascript in Page_Load of the child page, and in the immediate statement, I'm making use of the hidden variable value for processing. 我在子页面的Page_Load中调用javascript,在即时语句中,我正在使用隐藏变量值进行处理。 When I access hidden variable value, I'm getting default value only. 当我访问隐藏变量值时,仅获得默认值。

Please suggest me any of handling the scenario. 请建议我处理任何情况。 I need to execute the javascript and get the result into the hidden variable. 我需要执行javascript并将结果放入隐藏变量中。 Both in need to occur in Page_Load event only. 两者都只需要在Page_Load事件中发生。

Hidden Variable declaration: 隐藏变量声明:

<asp:HiddenField runat='server' ID='hdnDate' Value='0' />

Javscript: 字幕:

function getCDate() {
    var nowDate    = new Date();  
    var curr_month = nowDate.getUTCMonth();
    curr_month ++;
    var dt = curr_month + "/" + nowDate.getUTCDate() + "/" +nowDate.getFullYear()+ " " + nowDate.getUTCHours()+":" +nowDate.getUTCMinutes()+":" +nowDate.getUTCSeconds();
    document.getElementById("ctl00_ContentPlaceHolder1_hdnDate").value = dt;          
    return true;
}

Page_Load method in code behind file: 文件后面代码中的Page_Load方法:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "getCDate();", true);
    DateTime dt=Convert.ToDateTime(hdnDate.Value);
    dt.AddDays(10);    //getting error here because dt contains01/01/0001       
}

You cannot call javascript in Page_Load . 您不能在Page_Load调用javascript。 It is client side thing so should come from browser only. 它是客户端的东西,因此应该仅来自浏览器。 You can check if page is postback using IsPostBack property of Page object like this: 您可以使用Page对象的IsPostBack属性检查页面是否为回发,如下所示:

if(IsPostBack)
{
   //this is coming from browser so you javascript  might have been
   //called and proper value set in hidden field.
}

Javascript is run on the client side, so can't run in the Page_Load event on the Server. Javascript在客户端运行,因此无法在服务器上的Page_Load事件中运行。

From the looks of your code, I'm pretty sure you don't need javascript, you can just put the value into a SessionVariable as: 从代码的外观来看,我确定您不需要javascript,您可以将值放入SessionVariable中,如下所示:

Session.Add("DateRendered", DateTime.Now.AddDays(10).ToString("MM/dd/YYYY")); Session.Add(“ DateRendered”,DateTime.Now.AddDays(10).ToString(“ MM / dd / YYYY”));;

And then retrieve it later. 然后再检索。 ASP.net takes care of storing it in the Request/Response. ASP.net负责将其存储在请求/响应中。

RegisterStartupScript will register your block of script for the next page load. RegisterStartupScript将为下一个页面加载注册您的脚本块。 if you just want some value to be transferred to .cs page write a static method on the .cs and call it from javascript using PageMethods.MethodName(); 如果您只是想将某些值传输到.cs页,请在.cs上编写一个静态方法,然后使用PageMethods.MethodName()从javascript调用它;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM