aspxページを備えたASP.netアプリケーションがあります。
aspxページの場合、Page_Load
javascriptの非表示変数の値に基づくコードを処理しています(javascriptの結果を非表示変数に割り当てます)。
子ページのjavascriptを呼び出しておりPage_Load
、immediateステートメントでは、処理に非表示の変数値を使用しています。非表示の変数値にアクセスすると、デフォルト値のみが取得されます。
シナリオの処理について教えてください。javascriptを実行し、結果を非表示の変数に取り込む必要があります。Page_Load
両方ともイベントでのみ発生する必要があります。
隠れた変数の宣言:
<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メソッド:
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
}