我有一个带有 aspx 页面的 ASP.net 应用程序。
在Page_Load
aspx页面的情况下,我正在处理一些基于来自javascript的隐藏变量的值的代码(将javascript的结果分配给隐藏变量)。
我在Page_Load
子页面中调用 javascript,在立即声明中,我正在使用隐藏变量值进行处理。当我访问隐藏变量值时,我只获得默认值。
请建议我处理这种情况。我需要执行 javascript 并将结果放入隐藏变量中。两者都只需要在Page_Load
事件中发生。
隐藏变量声明:
<asp:HiddenField runat='server' ID='hdnDate' Value='0' />
Javascript:
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 方法:
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
}