简体   繁体   English

将.aspx加载到Panel / UpdatePanel中

[英]Loading .aspx into a Panel/UpdatePanel

I'm currently working on a web-based game using ASP.NET and C#. 我目前正在使用ASP.NET和C#开发基于Web的游戏。 Previously, I've been just organizing the various game screens(start, hi-score, results, etc) into individual Panels nested within a main UpdatePanel and showing/hiding the appropriate Panels as the game progresses. 以前,我只是将各种游戏屏幕(开始,高分,结果等)组织到嵌套在主UpdatePanel中的各个面板中,并随着游戏的进行显示/隐藏适当的面板。 However, I've severely underestimated the scale of the game and I'm gonna have to break it down to more manageable chunks. 但是,我严重低估了游戏的规模,我不得不将其细分为更易于管理的部分。

My idea now is to have a Main.aspx with an empty UpdatePanel and I'll load the various screens as individual .aspx files into it. 我现在的想法是拥有一个带有空UpdatePanel的Main.aspx,然后将各个屏幕作为单独的.aspx文件加载到其中。 Kinda like with WinForms I think, when you create another new WinForm and add it to the current parent form. 我认为,Kinda喜欢使用WinForms,当您创建另一个新的WinForm并将其添加到当前的父窗体中时。

I've been trying to find how to get about doing this in the code-behind, but I'm still at a lost. 我一直在尝试找到如何在后台代码中进行此操作的方法,但我仍然迷失了方向。 I could probably do this with Response.Redirect, but then there would be the usual flicker when the new page loads, which is something I want to avoid. 我可能可以使用Response.Redirect做到这一点,但是在新页面加载时会出现通常的闪烁,这是我要避免的事情。

I did come across something interesting called a UFrame but that seems to work in the HTML instead of the code-behind. 我确实遇到了一个有趣的东西,称为UFrame,但它似乎可以在HTML中而不是在背后的代码中工作。

I appreciate any help or suggestions in this matter, and apologize if this has been asked before. 感谢您在此问题上的任何帮助或建议,对于以前曾被要求的问题,我们深表歉意。 Thank you. 谢谢。

Edit: With regards to womp's answer, .ascx seem to be a step in the right direction for me. 编辑:关于womp的答案,.ascx对我来说似乎是朝正确方向迈出的一步。 I successfully got my Startscreen control control up and running inside the Main.aspx. 我成功地在Main.aspx中启动并运行了Startscreen控件。 However, another problem has popped up, where the screen just goes blank when I try to add another usercontrol to a panel inside Main.aspx upon a button click. 但是,弹出了另一个问题,当我尝试在单击按钮时向Main.aspx内的面板添加另一个用户控件时,屏幕只是一片空白。

The event handler I have for a button in the Startscreen control is: 我在“开始”屏幕控件中具有按钮的事件处理程序是:

protected void Btn_Arcade_Click(object sender, EventArgs e)
    {
        this.Parent.Controls.Add(LoadControl("Arcade.ascx"));

    }

I'm not too sure if there's anything wrong with that, but the Parent in this case is basically what I called the GameContentPanel that resides within Main.aspx's UpdatePanel. 我不太确定这是否有问题,但是在这种情况下,Parent基本上就是我所说的位于Main.aspx的UpdatePanel中的GameContentPanel。

I don't think it's a problem with the HTML fudging up either, since I tested it by loading it first instead of the Startscreen, and everything showed up fine. 我也不认为HTML出现问题,因为我通过先加载而不是加载Startscreen对其进行了测试,并且一切正常。

I've also tried loading both controls together at the start in Main.aspx, and both controls load correctly as well. 我还尝试过在Main.aspx开始时将两个控件一起加载,并且两个控件也正确加载。

Can the "screens" be .ascx usercontrols? “屏幕”可以是.ascx用户控件吗? You can load a usercontrol into a panel using LoadControl("~/path/to/myControl.ascx") , and then those controls can take part in the lifecycle of the main page. 您可以使用LoadControl("~/path/to/myControl.ascx")将用户控件加载到面板中,然后这些控件可以参与主页的生命周期。

If they really have to be in separate .aspx pages, then you might need to look at using an iframe, or else maybe rethinking your approach. 如果它们确实必须位于单独的.aspx页中,则您可能需要考虑使用iframe,否则可能需要重新考虑您的方法。 Webforms apps were designed to have lots of things happening inside the context of one page/form, and not transferring control back and forth to multiple pages. Webforms应用程序旨在在一页/表单的上下文中发生很多事情,而不是将控件来回传递到多个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM