C# 动态创建 LinkBut​​ton 命令事件处理程序

发布于 2024-07-06 23:21:11 字数 469 浏览 6 评论 0原文

所以我这里有一个奇怪的情况...我有一个 System.Web.UI.WebControls.WebParts.EditorPart 类。 它呈现一个“搜索”按钮,当您单击此按钮时,它的 clickHandler 方法会执行数据库搜索,并为其返回的每一行动态创建一个 LinkBut​​ton,设置 CommandName 和 CommandArgument 属性并添加 CommandEventHandler 方法,然后添加 LinkBut​​ton 控件到页面。

问题是,当您单击 LinkBut​​ton 时,它的 CommandEventHandler 方法永远不会被调用,看起来页面只是回发到按下原始“搜索”按钮之前的位置。

我看过一些帖子说您需要在 OnLoad() 或其他一些早期方法中添加事件处理程序,但直到用户告诉我们要搜索什么并点击“搜索”按钮之前,我的 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.

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.

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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小女人ら 2024-07-13 23:21:12

这是我最喜欢的技巧:)

我们的场景是首先渲染一个控件。 然后使用用户的一些输入,呈现进一步的控件并让它们响应事件。

这里的关键是状态——你需要知道控件到达PostBack时的状态——所以我们使用ViewState。 那么这个问题就变成了先有鸡还是先有蛋的问题。 ViewState 在 LoadViewState() 调用之后才可用,但您必须在该调用之前创建控件才能正确触发事件。

诀窍是重写 LoadViewState() 和 SaveViewState() 以便我们可以控制事物。

(请注意,下面的代码很粗糙,仅凭记忆,可能存在问题)

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]);
}

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.

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.

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

(note that the code below is rough, from memory and probably has issues)

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]);
}
最偏执的依靠 2024-07-13 23:21:12

您需要在加载中重新添加动态创建的控件,以便它们可以位于页面层次结构中并触发其事件。

You need to re-add the dynamically created controls, in the onload, so that they can be in the page hierarchy and fire their event.

桃气十足 2024-07-13 23:21:12
 LinkButton link= new LinkButton();
 link.Command +=new CommandEventHandler(LinkButton1_Command);

 protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    try
    {
        System.Threading.Thread.Sleep(300);
        if (e.CommandName == "link")
        {
           //////////
        }
    }
    catch
    {

    }
}
 LinkButton link= new LinkButton();
 link.Command +=new CommandEventHandler(LinkButton1_Command);

 protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    try
    {
        System.Threading.Thread.Sleep(300);
        if (e.CommandName == "link")
        {
           //////////
        }
    }
    catch
    {

    }
}
痴意少年 2024-07-13 23:21:12

我刚刚想出的一个肮脏的技巧是创建与真实按钮具有相同 ID 的虚拟 LinkBut​​ton。
假设您要在 Pre_Render 处创建一个 LinkBut​​ton“foo”(为时已晚),然后在 Page_Load 处创建一个虚拟 foo:(

        var link = new LinkButton();
        link.ID = "foo";
        link.Click += fooEventHandler;
        dummyButtons.Controls.Add(link);

其中“dummyButtons”只是页面上的一个占位符,其可见性设置为 false。)
它很丑,但很有效。

A dirty hack I just came up with, is to create dummy LinkButtons with the same IDs as the real buttons.
So let's say you are going to create a LinkButton "foo" at Pre_Render (which is too late), then also create a dummy foo at Page_Load:

        var link = new LinkButton();
        link.ID = "foo";
        link.Click += fooEventHandler;
        dummyButtons.Controls.Add(link);

(Where "dummyButtons" is just a PlaceHolder on the page with Visibility set to false.)
It's ugly, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文