在jQuery中,我如何等待ASP.NET Multiview通过PostBack更改其ActiveViewIndex?

时间:2011-04-25 21:19:50

标签: javascript jquery asp.net multiview

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

编辑:我忘了提到我在这个项目中使用了部分回传和UpdatePanel,并且在部分PostBacks上没有刷新JS。

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

如果您想查看我的客户端代码,请注意,但是请注意我对整个原型/命名空间的东西都是新手,如果它不正确,可以对我裸露,或者可以优化:


Global 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 );
}

2 个答案:

答案 0 :(得分:0)

不是直接调用page.init(),而是将它绑定到onLoad甚至是body。

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

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

答案 1 :(得分: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(); }          
    }
};

然后在我的策略登录视图JS 中,我将forgotPasswordLink.click事件更改为:

this.forgotPasswordLink.on
(
    "click", 

    function () 
    {     
        page.intervalId = setInterval
        ( 
            'page.waitForViewChange( "forgotpassword" )',
            300 
        ); 
    }
);