簡體   English   中英

ASP.NET 4.0 C#“狀態信息對此頁面無效,並且可能已損壞。” MasterPage-> Aspx-> Ascx

[英]ASP.NET 4.0 C# “state information is invalid for this page and might be corrupted.” MasterPage->Aspx->Ascx

這是我的第一篇SO帖子,因此,請原諒任何虛假行為。 我覺得好像我已經在與該錯誤有關的每個帖子上都嘗試了所有操作(禁用viewstate,noCache等),而且我處於機智。

我有一個空白項目,其中包含一個母版頁,一個頁面和一個控件。

該頁面(aspx)使用母版頁加載。 這很好。 頁面上有一個按鈕,用於將控件(ascx)加載到名為divRightMainView的部分的aspx上。 這也可以正常工作(這是我的問題似乎不同於我發現的所有其他問題的地方...)。 在ascx上有一個按鈕,該按鈕應該調用ascx的代碼隱藏-這是我收到“狀態信息”錯誤的地方。

這是aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"                   EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Outer.aspx.cs"   Inherits="TestProject.Outer" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script  type="text/javascript">
        function loadUserView(viewToLoad) {
            PageMethods.RenderControl(viewToLoad, onSuccess, onError);
        }

        function onSuccess(results) {
            alert(results);
            var command = results.split('##')[0];

            if (command == 'loadView') {
                var htmlToLoad = results.split('##')[1];
                $get('divRightMainContentView').innerHTML = htmlToLoad;
            }
        }

        function onError(error) {
            alert('error ' + error);
        }

    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
     <div id="leftSideContent">
         <input type='button' id="clickToLoadASCX" value="Click Me" onclick="loadUserView('/inner.ascx');"/>
         This is the "Outer.aspx" page which will hold the ascx after the button is clicked
     </div>
     <div id="divRightMainContentView">

     </div>
</asp:Content>

后面的Aspx代碼(呈現ascx)

 [WebMethod]
    public static string RenderControl(String controlName)
    {

        Page page = new Page();
        Control control = page.LoadControl(controlName);
        HtmlForm form = new HtmlForm();
        form.Controls.Add(control);
        page.Controls.Add(form);
        StringWriter writer = new StringWriter();
        HttpContext.Current.Server.Execute(page, writer, false);
        return "loadView##" + writer.ToString();
    }

這是稱為inner.ascx的ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="inner.ascx.cs" Inherits="TestProject.inner" %>

<div>
    This is the inner text. When I click the button below I should proceed to inner.ascx.cs to the "PageBehindCall_Submit".
    <asp:Button ID="innerButton" runat="server" onclick="PageBehindCall_Submit" Text="Submit"/> 
</div>

最后,ascx背后的代碼

protected void PageBehindCall_Submit(object sender, EventArgs e)
{
    string str = "This call does not work!";
}

我希望能夠使用“ PageBehindCall_Submit”處理數據,從ascx抓取輸入等。讓我知道是否有辦法做到這一點,或者還有其他可能的解決方法? 任何幫助,將不勝感激!

為什么不在ascx的頁面加載中加載ascx,而是關閉可見性而不是手動呈現用戶控件html。 這很奇怪,因為該控件屬於ascx,所以它沒有與ascx中的事件掛鈎。 但是我認為您應該在aspx中添加它,然后在需要時嘗試打開可見性。

暫無
暫無

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

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