Tags

,

If you have a usercontrol which has a button click event and you want to raise a postback like the button is clicked.

User Control code to declare a global variable in javdsciprt for Postback

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        Dim sm As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "ClientID", String.Format("var logoutClientId = '{0}$LogoutLink';", Me.ClientID.Replace("_", "$")), True)

    End Sub

    Public Sub LogoutLink_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LogoutLink.Click
        Page.LogoutCurrentUser("Sucessfully logged out.")
    End Sub

The below javascipt is to logout when a full postback raised.

window.onunload = function(){ unloadPageAndError();}

var loader = function(sender, args) { 
    if(!args.get_isPartialLoad()){
        PageMethods.CheckLogin(function(result){
            if(result)
            {
                unloadPageAndError();
            }    
        },
        function(error)
        {
            unloadPageAndError();
        }
        );    
    }
}

var unloadPageAndError = function()
{
    <strong>__doPostBack(logoutClientId,'')</strong>
}

Sys.Application.add_load(loader);

Below is a Web Method in your web page, you need to enable your scriptmanger.enablepagemethod = true

    <System.Web.Services.WebMethod()> _
    Public Shared Function CheckLogin() As Boolean
        Return UserContext() Is Nothing
    End Function
Advertisements