简体   繁体   English

ASP.NET Page_Load没有触发

[英]ASP.NET Page_Load is not firing

I created a 2D array Board , holding objects of GameCell . 我创建了一个2D数组Board ,持有GameCell对象。

Default.aspx: Default.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="includes/css/style.css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>ארבע בשורה</h1>
        <div id="Board">
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <asp:PlaceHolder ID="MyBoard" runat="server" />
        </div>
    </div>
    </form>
</body>
</html>

Default.aspx.cs: Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    Game.CreateControls();
}

protected void Page_Init(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Game.Set(MyBoard, Label1);
        Game.Reset();
    }
}

GameCell.cs: GameCell.cs:

public class GameCell : ImageButton
{
    public Position Position; // Justs holds the properties of Row and Col
    public CellType Type;

    public GameCell(Position Pos)
    {
        this.Position = new Position(Pos);
        this.Type = CellType.Empty;
    }
}

Game.cs: Game.cs:

public class Game
{
    public const int R = 6;
    public const int C = 9;

    public static GameCell[,] Board;
    public static PlaceHolder Holder;
    public static Label Log;

    public static CellType CurrentType;

    public static string[] Images = new string[] { "red", "empty", "blue" };

    public static void Set(PlaceHolder Holder, Label Log)
    {
        Game.Reset();
        Game.Holder = Holder;
        Game.Log = Log;
    }

    public static void CreateControls()
    {
        for (int i = 0; i < Game.R; i++)
        {
            for (int j = 0; j < Game.C; j++)
            {
                Game.Board[i, j].ImageUrl = Game.ImageUrlByType(Game.Board[i, j].Type);
                Game.Board[i, j].ID = i + "_" + j;
                Game.Board[i, j].Click += new ImageClickEventHandler(Image_Click);

                Game.Holder.Controls.Add(Game.Board[i, j]);
            }
        }
    }

    public static void Image_Click(object sender, EventArgs e)
    {
        int row = 0;
        int col = int.Parse(((GameCell)sender).ID[2].ToString());

        if (Game.Board[row, col].Type == CellType.Empty)
        {
            while (row < Game.R - 1 && Game.Board[row, col].Type == CellType.Empty)
            {
                row++;
            }

            Game.SetImage(row, col);
            Game.CurrentType = (CellType)(-(int)Game.CurrentType);
            Log.Text = col + " " + row + " " + Game.CurrentType;

            Game.Board[row, col].Enabled = false;
        }
    }

    public static void SetImage(int r, int c)
    {
        Game.Board[r, c].Type = Game.CurrentType;
        Game.Board[r, c].ImageUrl = Game.ImageUrlByType(Game.CurrentType);
    }

    public static void Reset()
    {
        Game.Board = new GameCell[R, C];
        Game.CurrentType = CellType.Blue;

        for (int i = 0; i < Game.R; i++)
        {
            for (int j = 0; j < Game.C; j++)
            {
                Game.Board[i, j] = new GameCell(new Position(i, j));
            }
        }
    }

    public static string ImageUrlByType(CellType t)
    {
        return "includes/images/" + Game.Images[(int)t + 1] + ".png";
    }
}

The controls are rendered, and are clickable when first launching the project, but after I do click one of them, no controls are added to my Holder ( MyBoard ). 控件是渲染的,并且在第一次启动项目时可以单击,但在我单击其中一个之后,我的Holder( MyBoard )中没有添加任何控件。

Why is this happening? 为什么会这样?

Edit: 编辑:

I found the solution. 我找到了解决方案。 passing MyBoard as an argument to CreateControls did it. MyBoard作为参数传递给CreateControls就可以了。 Maybe the reference wasn't reliable anymore (I saved it in an object) after page_load. 也许在page_load之后,引用不再可靠(我将其保存在对象中)。

Every time your Page_Load is called, you recreate your controls. 每次调用Page_Load ,都会重新创建控件。 They are stored in static members, which means that all users of this web site share a common board. 它们存储在静态成员中,这意味着该网站的所有用户共享一个公共板。 Is this really what you want? 这真的是你想要的吗? Would it not be better to store the Game in the Session? 将游戏存储在会话中会不会更好? (of course I don't know your requirements, but I always try to avoid static members in web applications). (当然我不知道你的要求,但我总是尽量避免在web应用程序中使用静态成员)。

A possible solution could be to check whether it is not a postback, before you recreate the controls. 在重新创建控件之前,可能的解决方案是检查它是否不是回发。

if (!IsPostback)
{

}

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

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