簡體   English   中英

我可以從httpModule訪問當前頁面的viewstate嗎

[英]Can I access curent page viewstate from httpModule

為了避免用戶單擊瀏覽器的刷新按鈕時重新發送請求的已知問題,

我決定添加一個HttpModule,在其中重寫load_Complete方法以將頁面重定向到其自身。 I基本上,請跟蹤HiddenField值,並在重定向后將其還原。

效果很好,現在的問題是重定向后頁面的視圖狀態數據丟失(這是預期的行為)。

所以,問題是,有沒有一種方法可以在重定向之前訪問頁面的viewstate數據(就像我對HiddenField控件所做的操作一樣_page.Controls中存在)? 也許從httpContext?

這是我的HttpModule的片段:

public void Init(HttpApplication context)
{
    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}

void context_PreRequestHandlerExecute(object sender, EventArgs e)
{               
    _httpContext = System.Web.HttpContext.Current;

    if (_httpContext != null) {
        _page = _httpContext.Handler as System.Web.UI.Page;

        if (_page != null) {
            _page.Load += new EventHandler(_page_Load);
            _page.LoadComplete += new EventHandler(_page_LoadComplete);
        }
        else { return; }

    }
    else { return; }
}

void _page_LoadComplete(object sender, EventArgs e)
{       
    if (_page.IsPostBack) {                       
        /*
        I Dump all hiddenfield values in 1 session variable
        */          
        //hoping to do the same for page's ViewState

        _httpContext.Response.Redirect(_httpContext.Request.RawUrl, false);
    }
}

void _page_Load(object sender, EventArgs e)
{   
    if (!_page.IsPostBack) {
        /*
        restore page hiddenfield controls
        */          
        //hoping to do the same for page's ViewState
    }
}

我最終添加了一個Base Page類,並更改了所有頁面以從此BasePage繼承。

在“基本”頁面中,我有2個公共方法, logViewState()restoreViewState() 基本上,這些方法將ViewState保存到會話中,並分別從會話中恢復ViewState。

_page_Load我調用logViewState() ,然后從_page_LoadComplete我調用logViewState()

希望這對某人有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM