Kjøre c# kode når et vindu lukkes

1 comment

Ved å bruke Ajax kan man kalle C# kode ved body.unload osv.
 
Koden her sjekker om en verdi er satt som indikerer at record er endret (document.getElementById('" + oppdatert.ClientID + "').value!='')
Hvis den er endret kalles c# metoden OppdaterMeny.
 
 
Sett enablepagemethods til true i scriptmanager:
Aspx-fil
<asp:ScriptManager ID="script1" runat="server" />
 
 
Javascript
 private void JavaScript()
    {
        StringBuilder script = new StringBuilder();
        script.Append("<script language='javascript'>");
        script.Append("function retVerdi(){window.returnValue=document.getElementById('" + oppdatert.ClientID + "').value;}");
        script.Append("function handleClose(){if(document.getElementById('" + oppdatert.ClientID + "').value!='') PageMethods.OppdaterMeny();}");         script.Append("</script>");
 
        Body.Attributes.Add("OnUnload", "handleClose(); retVerdi();");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "retVerdi", script.ToString());
    }
 
 
 
 
C# metode som kalles
[WebMethod]
public static void OppdaterMeny()
    {
     
      if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            CMSUtils cms = new CMSUtils();
            cms.SettCMSMenyFilter();
 
        }
    }
dvs at du trenger en
using System.Web.Services;
på toppen...
 
 
 
 
 

Posted by email from Henris blogg (posterous)

1 comment :

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete