[英]RegisterStartupScript only runs the first time. ASP.NET C# JavaScript
I have an .aspx page with the following code 我有一个带有以下代码的.aspx页面
<asp:Panel runat="server" ID="ImagePanel">
<asp:UpdatePanel runat="server" ID="ImageUpdatePanel" UpdateMode="Conditional"><ContentTemplate>
<img id="photo" src="/Icons/Factory Layout.png" style="display: none;"/>
<script type="text/javascript">
function ResetImage(typeOfImage) {
var factoryImage = $("#photo");
if (typeOfImage === 1) {
factoryImage.attr("src",document.getElementById('<%= FactoryImageFileNameHF.ClientID %>').value);
}
else if (typeOfImage === 2) {
factoryImage.attr("src",document.getElementById('<%= IncidentFactoryImageFileNameHF.ClientID %>').value);
}
It is the ResetImage javascript
function that I am trying to run with the following code in the code behind. 我正在尝试使用后面的代码中的以下代码来运行ResetImage
javascript
函数。
if (typeOfMap == 1)
{
FactoryImageFileNameHF.Value = fileNameOfFactoryImage.FileFullPath();
ClientScript.RegisterStartupScript(Page.GetType(), "test" + ScriptKeyHF.Value, "ResetImage(1);", true);
ScriptKeyHF.Value = (ScriptKeyHF.Value.ToInt() + 1).ToString();
}
else if (typeOfMap == 2)
{
IncidentFactoryImageFileNameHF.Value = fileNameOfFactoryImage.FileFullPath();
ClientScript.RegisterStartupScript(Page.GetType(), "test" + ScriptKeyHF.Value, "ResetImage(2);", true);
ScriptKeyHF.Value = (ScriptKeyHF.Value.ToInt() + 1).ToString();
}
The problem is that the ResetImage() method that I am calling in the RegisterStartUpScript
only runs the first time on the browser. 问题是我在
RegisterStartUpScript
调用的ResetImage()方法仅在浏览器上第一次运行。 It doesn't run the second and third time of postbacks, etc. I have tried the RegisterClientScriptBlock
but it runs before the javascript
code is there. 它不会运行第二次和第三次回发等。我已经尝试过
RegisterClientScriptBlock
但是它在javascript
代码在那里之前就运行了。 Does anyone have any idea why the code only runs the first time. 有谁知道为什么代码只在第一次运行。
The solution is that I was using a ClientScriptManager (ClientScript) instead of the a ScriptManager so I changed the line of code to 解决方案是我使用的是ClientScriptManager(ClientScript)而不是ScriptManager,所以我将代码行更改为
System.Web.UI.ScriptManager.RegisterStartupScript(this.Page,Page.GetType(), "test" + ScriptKeyHF.Value, "ResetImage(1);", true);
and it works. 而且有效。
I have the ScriptManager on a Master page and my code was on a child page 我在母版页上有ScriptManager,而我的代码在子页上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.