【发布时间】:2019-01-02 14:47:00
【问题描述】:
我有这个WebFormHtml:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetLink.aspx.cs" Inherits="GetLink" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" runat="server" id="hdnVal" value="55"/>
</div>
</form>
</body>
</html>
我想在这段代码中添加一个JavaScript 函数并使用这段代码运行它:
protected void Page_Load(object sender, EventArgs e)
{
if (!ClientScript.IsStartupScriptRegistered("key1"))
{
ClientScript.RegisterStartupScript(GetType(), "key1", @"<script type=""text/javascript"">function callMyJSFunction() { document.getElementById(""hdnVal"").value='5'; }</script>");
}
ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction();</script>");
string resutOfExecuteJavaScript = hdnVal.Value;
}
当我运行它时,hdnVal 的值保持55 的值不变。知道是什么问题吗?
【问题讨论】:
-
仔细研究this页面,你需要的一切都在那里。特别注意多次注册同一个key的脚本部分。