C#动态创建LinkBut​​ton的指挥事件处理程序 [英] C# Dynamically created LinkButton Command Event Handler

查看:83
本文介绍了C#动态创建LinkBut​​ton的指挥事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里有一个奇怪的情况...我有一个System.Web.UI.WebControls.WebParts.EditorPart类。它呈现一个搜索按钮,当你点击这个按钮,它的clickHandler方法,做一个数据库的搜索,并动态地为它返回的每一行创建一个LinkBut​​ton,设置的CommandName和CommandArgument性能并增加了一个CommandEventHandler方法,然后添加LinkBut​​ton控件到页面上。

So I have a weird situation here... I have an System.Web.UI.WebControls.WebParts.EditorPart class. It renders a "Search" button, when you click this button, it's clickHandler method does a DB search, and dynamically creates a LinkButton for each row it returns, sets the CommandName and CommandArgument properties and adds a CommandEventHandler method, then adds the LinkButton control to the page.

问题是,当你点击一个LinkBut​​ton,其CommandEventHandler方法不会被调用,它看起来像页面只是职位回到它是原来的搜索按钮之前是pressed。

The problem is, when you click a LinkButton, its CommandEventHandler method is never called, it looks like the page just posts back to where it was before the ORIGINAL "Search" button was pressed.

我看到帖子说你需要添加的OnLoad)事件处理程序(或其他一些早期的方法,但我LinkBut​​tons甚至还没有被创建,直到用户告诉我们什么搜索和点击搜索按钮...如何处理这个任何想法?

I have seen postings saying that you need to add the event handlers in OnLoad() or some other early method, but my LinkButtons haven't even been created until the user tells us what to search for and hits the "Search" button... Any ideas on how to deal with this?

谢谢!

推荐答案

这是我最喜欢的把戏:)

This is my favorite trick :)

我们的方案是首先呈现的控制。然后使用一些来自用户的输入,使得进一步的控制,让他们响应事件。

Our scenario is to first render a control. Then using some input from the user, render further controls and have them respond to events.

这里的关键是状态 - 你需要知道控件的状态,当它到达回传 - 所以我们使用的ViewState。那么问题就变成了一个鸡和蛋的问题; ViewState中不可用,直到 LoadViewState()调用后,但你必须是调用有正确的事件之前发射创建控件。

The key here is state - you need to know the state of the control when it arrives at PostBack - so we use ViewState. The issue becomes then a chicken-and-egg problem; ViewState isn't available until after the LoadViewState() call, but you must create the controls before that call to have the events fired correctly.

关键是要覆盖 LoadViewState() SaveViewState(),以便我们能够控制的事情。

The trick is to override LoadViewState() and SaveViewState() so we can control things.

(注意:code以下是粗糙,存储器和可能有问题)

private string searchQuery = null;

private void SearchButton(object sender, EventArgs e)
{
    searchQuery = searchBox.Text;
    var results = DataLayer.PerformSearch(searchQuery);
    CreateLinkButtonControls(results);
}

// We save both the base state object, plus our query string.  Everything here must be serializable.
protected override object SaveViewState()
{
    object baseState = base.SaveViewState();
    return new object[] { baseState, searchQuery };
}

// The parameter to this method is the exact object we returned from SaveViewState().
protected override void LoadViewState(object savedState)
{
    object[] stateArray = (object[])savedState;

    searchQuery = stateArray[1] as string;

    // Re-run the query
    var results = DataLayer.PerformSearch(searchQuery);

    // Re-create the exact same control tree as at the point of SaveViewState above.  It must be the same otherwise things will break.
    CreateLinkButtonControls(results);

    // Very important - load the rest of the ViewState, including our controls above.
    base.LoadViewState(stateArray[0]);
}

这篇关于C#动态创建LinkBut​​ton的指挥事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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