click事件之后按钮阵列消失 [英] Button array disappears after click event

查看:167
本文介绍了click事件之后按钮阵列消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的按钮(按钮的数组)消失我点击其中的任何后?这里是code结构。感谢很多提前。

 公共部分类Seatalloc2:System.Web.UI.UserControl
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            如果(!的IsPostBack)
            {
                PopulateControls();
            }        }        保护无效PopulateControls()
        {            按钮[,] buttonArray =新的Button [10,14];
            为(int类型的= 0;一个小于10;一个++)
            对于(INT B = 0; B< 14; b ++)
            {
               buttonArray [A,B] =新的Button();
               Panel2.Controls.Add(buttonArray [A,B]);
             }        }        公共无效buttonHandler(对象发件人,EventArgs的发送)
        {
            按钮BTN =发件人的按钮;
            btn.BackColor = Color.Red;
        }
    }


解决方案

如果你看看我的回答你的最后一个问题,你会发现一个例子解决这个问题:

<一个href=\"http://stackoverflow.com/a/11061782/1268570\">http://stackoverflow.com/a/11061782/1268570

问题的根源是理解ASP.Net页面生命周期(我恨它),但它是有用的,关键要了解基本知识背后

这从微软的文档详细解释了页面生命周期

http://msdn.microsoft.com/en-us/library/ ms178472.aspx

基本上控制正在逐渐消失,因为你需要在每次回发的页面再重新创建,并在code,你只创建它们第一次加载你的页面

建议的事件来创建动态控件是 preINIT ,如果你没有一个母版页或初始化如果你有一个母版页

所以,如果你改变了code为:

 无效Page_Init(对象发件人,EventArgs的发送)
{
   PopulateControls();
}

您按钮可以节省自己的状态。不要担心状态,在每一个岗位,即使他们重新创建,因为你正在做它在初始化事件,ASP.Net将加载的ViewState 自动到你的控制(因为ASP.Net加载初始化事件之后和<$ C前的视图状态,这是可能的$ C>加载事件)

作为一个快速参考看看的页面生命周期:

Why do my buttons (array of buttons) disappear after I click any of them? Here is the code structure. Thanks a lot in advance.

public partial class Seatalloc2 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControls();
            }

        }

        protected void PopulateControls()
        {

            Button[,] buttonArray = new Button[10, 14];
            for (int a = 0; a < 10; a++)
            for (int b = 0; b < 14; b++)
            { 
               buttonArray[a, b] = new Button();
               Panel2.Controls.Add(buttonArray[a, b]);
             } 

        }

        public void buttonHandler(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.BackColor = Color.Red;           
        } 
    }

解决方案

If you look at my answer to your last question you will find an example addressing this issue:

http://stackoverflow.com/a/11061782/1268570

The root problem is understanding the ASP.Net page life-cycle (I hate it) but it is useful and crucial to understand the basics behind it

This documentation from Microsoft explains in detail the page life cycle

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Basically the controls are disappearing because you need to re-create them again in the page with each postback, and in your code, you are only creating them the first time you load your page

The recommended event to create dynamic controls is the PreInit if you do not have a master page or the Init if you do have one master page

So if you change your code to:

void Page_Init(object sender, EventArgs e)
{
   PopulateControls();
}

Your buttons will conserve their state. Do not worry about the state, even when they are re-created in each post, since you are doing it in the Init event, ASP.Net will load the ViewState automatically to your controls (this is possible because ASP.Net loads the view state after the Init event and before the Load event)

As a quick reference take a look at the page life-cycle:

这篇关于click事件之后按钮阵列消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆