如何在回发时保留动态控件 [英] how to retain dynamic controls on postback

查看:66
本文介绍了如何在回发时保留动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个动态表,其中动态按钮作为单元格,并且保存按钮也是动态的,页面回发或单击任何按钮时,表都消失了,如何保留它?

I have a created a dynamic table with dynamic buttons as cells and save button is also dynamic,on page postback or on any button click table disapears, how to retain it?

推荐答案

动态控件在回发后将消失,以解决回发后重新绑定的问题.
例如

dynamic controls will disappear after postback, to solve that issue rebind after postback.
for example

if (!IsPostBack)
        {
            Button button = new Button();
            button.ID = "Button1";
            PlaceHolder1.Controls.Add(button);
        }
        else
        {
            if (PlaceHolder1.FindControl("Button1") != null)
            {
                //bind your control
                Button button = new Button();
                button.ID = "Button1";
                PlaceHolder1.Controls.Add(button);
            }
        }


对于数据绑定表(或类似数据),您需要记录在viewstate中创建的行数,并在Page Load事件中重新创建它们.

要注意的一件事是确保回发后每个控件的唯一标识相同,否则视图状态将无法正确还原.
For a databound table (or similar) you''ll need to record how many rows you''ve created in viewstate and recreate them in the Page Load event.

One thing to watchout for is making sure that the uniqueid for each control is the same after the postback, otherwise the viewstate wont be restored properly.


这篇关于如何在回发时保留动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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