![](/img/trans.png)
[英]Unable to access codebehind variable in javascript in the aspx page
[英]Cannot run a Javascript code from Codebehind in an Aspx page
嗨,我有一個Web表單,必須在codebehind文件中調用javascript函數。 在向頁面添加更新面板之前,它一直運行良好。 我使用的代碼是
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "OpenExe()", true);
經過研究,我知道如果在頁面中添加腳本管理器/更新面板,則必須使用
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Sample", "OpenExe()", true);
我用了這個,我也用了
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"var cmdShell = new ActiveXObject('WScript.Shell');");
sb.Append(@"var myPath = '\'C:\\Temp\\stacks.exe\\'');");
sb.Append(@"</script>");
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
}
我也試過
ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "Sample", "OpenExe()", true);
但是javascript無法執行。 有人可以告訴我代碼有什么問題嗎? 如果您想查看更多代碼,請告訴我。 預先感謝
單擊確定,如果有“更新”面板,請嘗試以下方法:
ScriptManager.RegisterStartupScript(yourupdatepanel, this.GetType(), "Sample", "OpenExe()", true);
將您的updatepanel控件ID放在第一個參數上
我認為您需要將updatepanel的updatemode設置為“ always”。 或者,如果代碼包含在按鈕單擊處理程序中,則可以為updatepanel添加帶有按鈕的controleid attr的觸發器。
<asp:UpdatePanel ID="upnlDetailsScratchCard" runat="server"> <ContentTemplate> </ContentTemplate> <Triggers><asp:PostBackTrigger ControlID="buttonID" /> </Triggers> </asp:UpdatePanel>
<asp:UpdatePanel ID="upnlDetailsScratchCard" runat="server" UpdateMode="Always">
<ContentTemplate>
</ContentTemplate>
Java腳本代碼僅在整個程序執行后執行。 如果您的C#代碼中有任何錯誤,則從后面的代碼調用的腳本也將不起作用。 一個事件僅執行一個腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.