如何创建按钮和挂钩的回发事件 [英] How can I create buttons and hook up events from postback

查看:179
本文介绍了如何创建按钮和挂钩的回发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成最初是基于相当的处理器和磁盘密集型的搜索按钮。每个按钮将重新present选择和触发回发。我的问题是,回发不会触发命令b_Command。我猜是因为原来的按钮没有被重新创建。我不能affort在回发来重新创建按钮执行原来的搜索,所以我想生成回发信息所需的按钮。

在何处及如何768,16我可以这样做?我应该做的Page_Load之前的例子吗?从回传我如何重新构建CommandEventHandler - ?如果在所有

 命名空间CloudNavigation
{
    公共部分类测试:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的)
        {
            如果(的IsPostBack)
            {
                //我怎么可以重新生成按钮,并在此挂钩的事件
                //不执行重搜索1
            }
            其他
            {
                //执行重搜索1生成按钮
                按钮B =新按钮();
                b.Text =选择1;
                b.Command + =新CommandEventHandler(b_Command);
                Panel1.Controls.Add(B);
            }
        }

        无效b_Command(对象发件人,CommandEventArgs E)
        {
            //执行重搜索2,产生新的按钮
            按钮B2 =新按钮();
            b2.Text =选择2;
            b2.Command + =新CommandEventHandler(b_Command);
            Panel1.Controls.Add(B2);
        }
    }
}
 

解决方案

在b_Command事件处理方法不被因为交后退按钮没有被重新执行(因为它们是动态生成的)。你需要在每个页面被重建时重新创建它们,但为了做到这一点,你需要明确缓存状态的地方的信息。

如果此页面范围的操作最简单的方法是将其存储在ViewState(字符串 - 如果你开始加载使用对象的ViewState中,你会看到性能下降),这样你可以检查它旁边的负载(或任何其他previous事件),并重新加载页面时重新创建按钮。 如果操作会话范围,你可以很容易的对象(数组或其他)存储在会话和检索它的下一个负荷(或init)来重新创建控件。

该方案意味着,你只需要存储创建和添加按钮的关于你的按钮一些信息在你的b_Command事件处理程序,而不是因为如果你这样做,你会失去在接下来的回传相关信息(因为它是现在发生的事情)。

让你的code会变成这样的:

 命名空间CloudNavigation
{
    公共部分类测试:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的)
        {
            如果(的IsPostBack)
            {
                this.recreateButtons();
            }
            其他
            {
                //执行重搜索1生成按钮
                按钮B =新按钮();
                b.Text =选择1;
                b.Command + =新CommandEventHandler(b_Command);
                Panel1.Controls.Add(B);
                //专卖店这个东西在ViewState中的第一次
            }
        }

        无效b_Command(对象发件人,CommandEventArgs E)
        {
            //执行重搜索2,产生新的按钮
            // TODO:将数据存到ViewState中或会话
            //也许创造一些新的按钮
        }

        无效recreateButtons()
        {
            //从ViewState中或会话中检索数据,并创建所有的按钮
            //把它们连接起来,以事件处理程序
        }
    }
}
 

如果你不想调用页面加载recreateButtons你可以做到这一点在preLOAD或初始化事件,我看不出差别,因为你就可以到处访问的ViewState / Session变量(在初始化未应用视图状态,但你可以访问它来重新创建动态按钮)。

有人会恨这个解决方案,但据我所知,以保持状态数据的服务器端的唯一方法是的ViewState - 会话 - Page.Transfer或客户端饼干。

I need to generate buttons initially based on quite a processor and disk intensive search. Each button will represent a selection and trigger a postback. My issue is that the postback does not trigger the command b_Command. I guess because the original buttons have not been re-created. I cannot affort to execute the original search in the postback to re-create the buttons so I would like to generate the required button from the postback info.

How and where shoud I be doing this? Should I be doing it before Page_Load for example? How can I re-construct the CommandEventHandler from the postback - if at all?

   namespace CloudNavigation
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                // how can I re-generate the button and hook up the event here
                // without executing heavy search 1
            }
            else
            {
                // Execute heavy search 1 to generate buttons
                Button b = new Button();
                b.Text = "Selection 1";
                b.Command += new CommandEventHandler(b_Command);
                Panel1.Controls.Add(b);
            }
        }

        void b_Command(object sender, CommandEventArgs e)
        {
            // Execute heavy search 2 to generate new buttons
            Button b2 = new Button();
            b2.Text = "Selection 2";
            b2.Command += new CommandEventHandler(b_Command);
            Panel1.Controls.Add(b2);
        }
    }
}

解决方案

The b_Command Event Handler method is not being executed because on post back buttons are not being recreated (since they are dynamically generated). You need to re-create them every time your page gets recreated but in order to do this you need to explicitly cache information somewhere in state.

If this a page-scoped operation easiest way is to store it in the ViewState (as strings - if you start loading the ViewState with objects you'll see performance go down) so that you can check it on next load (or any other previous event) and re-create buttons when reloading the page. If the operation is session-scoped, you can easily store an object (array or whatever) in session and retrieve it on next Load (or Init) to re-create your controls.

This scenario means that you need just to store some info about your button in your b_Command EventHandler instead of creating and adding buttons since if you do so you'll lose relative information in the next postback (as it is happening now).

so your code would become something like:

namespace CloudNavigation
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                this.recreateButtons();
            }
            else
            {
                // Execute heavy search 1 to generate buttons
                Button b = new Button();
                b.Text = "Selection 1";
                b.Command += new CommandEventHandler(b_Command);
                Panel1.Controls.Add(b);
                //store this stuff in ViewState for the very first time
            }
        }

        void b_Command(object sender, CommandEventArgs e)
        {
            //Execute heavy search 2 to generate new buttons
            //TODO: store data into ViewState or Session
            //and maybe create some new buttons
        }

        void recreateButtons()
        {
            //retrieve data from ViewState or Session and create all the buttons
            //wiring them up to eventHandler
        }
    }
}

If you don't want to call recreateButtons on page load you can do it on PreLoad or on Init events, I don't see a difference since you'll be able to access ViewState/Session variables everywhere (on Init viewstate is not applied but you can access it to re-create your dynamic buttons).

Someone will hate this solution but as far as I know the only way to retain state data server-side is ViewState - Session - Page.Transfer or client-side cookies.

这篇关于如何创建按钮和挂钩的回发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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