Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在子页面中调用javascript';s onload事件_Asp.net_Javascript_Pageload - Fatal编程技术网

Asp.net 如何在子页面中调用javascript';s onload事件

Asp.net 如何在子页面中调用javascript';s onload事件,asp.net,javascript,pageload,Asp.net,Javascript,Pageload,我有一个带有aspx页面的ASP.net应用程序 在aspx页面的Page\u Load事件中,我正在处理一些基于javascript隐藏变量值的代码(将javascript的结果分配给隐藏变量) 我在子页面的Page\u Load中调用javascript,在immediate语句中,我使用隐藏变量值进行处理。当我访问隐藏变量值时,我只得到默认值 请建议我如何处理这种情况。我需要执行javascript并将结果放入隐藏变量中。这两种情况都需要在Page\u Load事件中发生 隐藏变量声明:

我有一个带有aspx页面的ASP.net应用程序

在aspx页面的
Page\u Load
事件中,我正在处理一些基于javascript隐藏变量值的代码(将javascript的结果分配给隐藏变量)

我在子页面的
Page\u Load
中调用javascript,在immediate语句中,我使用隐藏变量值进行处理。当我访问隐藏变量值时,我只得到默认值

请建议我如何处理这种情况。我需要执行javascript并将结果放入隐藏变量中。这两种情况都需要在
Page\u Load
事件中发生

隐藏变量声明:

<asp:HiddenField runat='server' ID='hdnDate' Value='0' />
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;
}
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       
}
页面\u代码隐藏文件中的加载方法:

<asp:HiddenField runat='server' ID='hdnDate' Value='0' />
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;
}
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       
}

您不能在
页面加载
中调用javascript。它是客户端的东西,所以应该只来自浏览器。您可以使用
page
对象的
IsPostBack
属性检查页面是否回发,如下所示:

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

Javascript在客户端运行,因此无法在服务器上的Page_Load事件中运行

从您的代码外观来看,我非常确定您不需要javascript,您只需将值放入SessionVariable中,如下所示:

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


然后稍后检索它。ASP.net负责将其存储在请求/响应中。

RegisterStartupScript将为下一页加载注册您的脚本块。
如果您只想将一些值传输到.cs页面,请在.cs上编写一个静态方法,并使用PageMethods.MethodName()从javascript调用它

请重申您的问题并显示一些代码(在页面加载和javascript中,您正在使用这些代码为隐藏字段赋值)。有人可以重新格式化代码吗?