Aspx->Ascx-6ren"> Aspx->Ascx-这是我的第一篇 SO 帖子,所以请原谅任何失礼。我觉得我已经尝试了与此错误相关的所有帖子(禁用 View 状态、noCache 等),但我已经束手无策了。 我有一个包含单个母版页、单个页面和单个控件的-6ren">
gpt4 book ai didi

c# - ASP.NET 4.0 C# "state information is invalid for this page and might be corrupted."MasterPage->Aspx->Ascx

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:19 25 4
gpt4 key购买 nike

这是我的第一篇 SO 帖子,所以请原谅任何失礼。我觉得我已经尝试了与此错误相关的所有帖子(禁用 View 状态、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 中的事件 Hook 。但我认为您应该将它添加到 aspx 中,然后在需要时尝试打开可见性。

关于c# - ASP.NET 4.0 C# "state information is invalid for this page and might be corrupted."MasterPage->Aspx->Ascx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14539585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com