2

我在 ASP.NET 3.5 中使用 VB.NET 作为我的服务器端语言。我通过 jQuery 1.5.2 使用 JavaScript 作为我的客户端语言。我正在尝试使用此页面进行原型/命名空间设置。此页面是使用 MultiView 的自定义登录页面。我有一个策略登录视图和一个忘记密码视图。我将 ActiveViewIndex 存储在一个名为currentView.

我在这个项目中使用带有 UpdatePanel 的 Partial Postbacks,并且 JS 不会在 Partial PostBacks 上刷新。

也就是说,我需要一种方法来等待 ActiveViewIndex 更改服务器端,以便我可以将init()函数调用到相应的视图。这可能吗?

我的客户端代码:

全球JS


var page = 
{                   
    currentView : getContentElement( "currentView" ),
    
    init : function () 
    { 
        log( "initializing page" );
                            
        if ( this.currentView === "forgotpassword" ) { forgotPassword.init(); }
        else { policyLogon.init(); }    
    }
}; 

page.init();

策略登录视图 JS


var policyLogon = 
{                       
    panel              : getContentElement( "pnlLogonInfo", "div" ),
    submitButton       : getContentElement( "btnLogon", this.panel ),
    textInputs         : $( "input[ type=text ]", this.panel ),
    policyNumber       : getContentElement( "txtPolicyNumber", this.textInputs ),
    policyNumberError  : getContentElement( "lblPolicyNumberError", this.panel ),
    password           : getContentElement( "txtPassword", this.textInputs ),
    passwordError      : getContentElement( "lblPasswordError", this.panel ),
    forgotPasswordLink : getContentElement( "lbtnForgotPassword", this.panel ),
                    
    init : function () 
    { 
        log( "initializing policy logon" );
                        
        var that = this;

        // Other event handlers created here [redacted to keep example code short].

        that.forgotPasswordLink.on("click", function () 
        {
            // Code to wait for PostBack should go here.

            page.init();
        });

        that.policyNumber.focus();              
    }
};

忘记密码查看JS


var forgotPassword = 
{
                            
    // Local vars, set up the same way as Policy Logon, just different elements.
    // [redacted to keep example code short]
                        
    init : function () 
    { 
        log( "initializing forgot password" );
                            
        var that = this;
    
        // Other event handlers created here [redacted to keep example code short].
    
        that.policyNumber.focus();                      
    }    
};


getContentElement()作为对什么是问题的先发制人的打击,getContentElement()由于嵌套的母版页,我必须创建一个函数,其中元素必须根据他们使用的母版页以不同的方式调用。它看起来像这样:

function getContentElement ( id, context ) 
{ 
    var publicPrefix = csMasterPrefix + csContentPrefix,
        securePrefix = cpMasterPrefix + publicPrefix + cpContentPrefix,
        publicId = "#" + publicPrefix + id,
        secureId = "#" + securePrefix + id;
    
    return ( context )
        ? ( isSecurePage ) ? $( secureId, context ) : $( publicId, context )
        : ( isSecurePage ) ? $( secureId ) : $( publicId );
}
4

2 回答 2

0

好的......所以我回答了我的问题,但没有什么特别的。我想我可以只使用 setInterval 并不断检查该page.currentView属性。而不是连续存储它,它总是未定义的,除非page.waitForViewChange最初设置它。

将我的全局 JS更改为:

var page = 
{                               
    currentView : undefined,
    intervalId  : undefined,

    init : function () 
    { 
        log( "init() fired" );

        this.currentView = getContentElement( "currentView" ).val();
        log( "current view is [" + this.currentView + "]" );

        if ( this.currentView === "forgotpassword" ) { forgotPassword.init(); }
        else { policyLogon.init(); }

        clearInterval( this.intervalId );
        this.intervalId = undefined;
        this.currentView = undefined;
    },

    waitForViewChange : function ( viewToWaitFor ) 
    { 
        log ( "waitForViewChange() fired" );

        this.currentView = getContentElement( "currentView" ).val();
        if ( this.currentView === viewToWaitFor ) { this.init(); }          
    }
};

然后在我的Policy Logon View JS中,我将forgotPasswordLink.click事件更改为:

this.forgotPasswordLink.on
(
    "click", 

    function () 
    {     
        page.intervalId = setInterval
        ( 
            'page.waitForViewChange( "forgotpassword" )',
            300 
        ); 
    }
);
于 2011-04-26T00:12:30.847 回答
0

与其直接调用 page.init() ,不如将其绑定到正文的 onLoad 。

$(document).ready() { page.init() }

此时,DOM已经从服务器返回,并且ActiveIndexItem也已经设置好了。

于 2011-04-25T21:28:41.050 回答