简体   繁体   中英

Calling a javascript method from the code behind

I need to call a JavaScript method with parameters from the code behind.

Javascript method

  <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.

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

ClientScriptManager.RegisterStartupScript Method

please check the link given below:

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>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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