简体   繁体   English

从后面的代码调用javascript方法

[英]Calling a javascript method from the code behind

I need to call a JavaScript method with parameters from the code behind. 我需要使用后面的代码中的参数调用JavaScript方法。

Javascript method Javascript方法

  <script type="text/javascript">
       function changeControlSample(path) 
       {
          $find('<%= PartialUpdatePanel7.ClientID %>').set_UserControlPath(path);
          $find('<%= PartialUpdatePanel7.ClientID %>').refresh();
       }
    </script>
<iucon:PartialUpdatePanel runat="server" ID="PartialUpdatePanel7" 
                        DisplayLoadingAfter="500" InitialRenderBehaviour="Clientside" EncryptUserControlPath="false">
                        <LoadingTemplate>

                             <div style="margin-left: 84px; margin-top: 10px;">
                                <asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" />
                            </div>
                            <div style="text-align: center">
                                Updating...
                            </div>
                        </LoadingTemplate>
                    </iucon:PartialUpdatePanel>

The code Behind of the page 页面背后的代码

protected Consultation controlconsultation  = new Consultation();
protected void Page_Load(object sender, EventArgs e)
        {
            PartialUpdatePanel7.UserControlPath = "Espace_Candidat/Consultation.ascx";
           controlconsultation.imageinfo += controlconsultation_imageinfo;
           Session["controlconsultation"] = controlconsultation;
        }
    void controlconsultation_imageinfo(object sender, CommandEventArgs e) 
    {
       PartialUpdatePanel7.UserControlPath = "Espace_Candidat/InfoEdition.ascx";
       Page.ClientScript.RegisterStartupScript(this.GetType(), 
                                              "CallMyFunction",   
        "changeControlSample('Espace_Candidat/InfoEdition.ascx')", true);
    }

Code behind of the user control 用户控件的代码

public event CommandEventHandler imageinfo ; 
protected void Page_Load(object sender, EventArgs e)
        {
 Consultation current = (Consultation)Session["controlconsultation"];
                imageinfo = current.imageinfo;
       }
  protected void Valider (object sender, CommandEventArgs e)
          {
            if (imageinfo != null)
              {
                  string pageNumber = (string)e.CommandArgument;
                  CommandEventArgs args = new CommandEventArgs("Control", pageNumber);
                  imageinfo(this, args);
              }
          }

This call didn't work even I change the JavaScript method by another one. 即使我用另一个方法更改JavaScript方法,此调用也不起作用。

For example, if I try 例如,如果我尝试

Page.ClientScript.RegisterStartupScript
(this.GetType(), 
"CallMyFunction", 
"alert('blabla');", 
 true);

I got the same result. 我得到了相同的结果。

  1. So, What is the error that I commited? 那么,我提出的错误是什么?
  2. How can I fix my code? 我该如何修复我的代码?

If you wish to call JavaScript method with parameters from the code behind you can get this done using 如果您希望使用后面的代码中的参数调用JavaScript方法,则可以使用此方法完成此操作

ClientScriptManager.RegisterStartupScript Method ClientScriptManager.RegisterStartupScript方法

please check the link given below: 请查看以下链接:

http://msdn.microsoft.com/en-us/library/z9h4dk8y(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/z9h4dk8y(v=vs.110).aspx

Hope this helps. 希望这可以帮助。

If you have update panel in page then call like this, 如果你有页面中的更新面板,那么这样打电话,

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), Guid.NewGuid().ToString(), @"<script type='text/javascript'>changeControlSample('" + path + "');</script>", false);

It don't have update panel then call like this 它没有更新面板,然后像这样调用

Page.ClientScript.RegisterStartupScript(this.GetType(), "tabselect", "<script type='text/javascript'>changeControlSample("' + path  + '");</script>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM