ASP.NET:处理回发而不被解雇的事件 [英] ASP.NET: Postback processed without events being fired

查看:203
本文介绍了ASP.NET:处理回发而不被解雇的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点击时应该解雇命令事件动态创建图像按钮的GridView。事件处理基本上的作品,除了第一次一个按钮被点击。然后,回传被处理,但该事件不会被解雇。

I have a GridView with dynamically created image buttons that should fire command events when clicked. The event handling basically works, except for the very first time a button is clicked. Then, the postback is processed, but the event is not fired.

我试图调试这一点,在我看来,那code之前执行,并首次点击后,是完全一样的其他任何点击。 (除了在第一次点击,事件处理程序不被调用。)

I have tried to debug this, and it seems to me, that the code executed before and after the first click is exactly the same as for any other clicks. (With the exception that in the first click, the event handler is not called.)

有一些特点在于:其中触发事件的按钮动态地通过数据绑定创建的,即数据绑定必须在页面的生命周期的两倍进行:一旦负载,以使存在的按钮(否则,事件可以不是在所有的,一旦事件被处理之后,以显示新的数据呈现之前处理)。

There is some peculiarity in that: The buttons which fire the event are created dynamically through databinding, i.e. databinding must be carried out twice in the page lifecycle: Once on load, in order to make the buttons exist (otherwise, events could not be handled at all), and once before rendering in order to display the new data after the events have been processed.

我看过这些帖子,但他们不会匹配我的情况:
<一href=\"http://stackoverflow.com/questions/1953448/asp-net-linkbutton-onclick-event-is-not-working-on-home-page\">http://stackoverflow.com/questions/1953448/asp-net-linkbutton-onclick-event-is-not-working-on-home-page,
<一href=\"http://stackoverflow.com/questions/96837/linkbutton-not-firing-on-production-server\">http://stackoverflow.com/questions/96837/linkbutton-not-firing-on-production-server,
<一href=\"http://stackoverflow.com/questions/953093/asp-net-click-event-doesnt-fire-on-second-postback\">http://stackoverflow.com/questions/953093/asp-net-click-event-doesnt-fire-on-second-postback

I have read these posts but they wouldn't match my situation: http://stackoverflow.com/questions/1953448/asp-net-linkbutton-onclick-event-is-not-working-on-home-page, http://stackoverflow.com/questions/96837/linkbutton-not-firing-on-production-server, http://stackoverflow.com/questions/953093/asp-net-click-event-doesnt-fire-on-second-postback

的细节:
GridView控件包含每行图像按钮。按钮的图像数据绑定。该行是通过GridView.DataBind产生的()。要做到这一点,我已经使用了自定义的ItemTemplate实施的TemplateField。 ItemTemplate中的InstantiateIn方法创建的ImageButton并为其分配根据事件处理程序。此外,图像的数据绑定事件分配检索基于各行的数据相应的图像处理程序。

To the details: The GridView contains image buttons in each row. The images of the buttons are databound. The rows are generated by GridView.DataBind(). To achieve this, I have used the TemplateField with a custom ItemTemplate implementation. The ItemTemplate's InstantiateIn method creates the ImageButton and assigns it the according event handler. Further, the image's DataBinding event is assigned a handler that retrieves the appropriate image based on the respective row's data.

GridView控件放置在用户控件。该用户控件定义GridView的事件,事件处理程序。在code大致如下所示:

The GridView is placed on a UserControl. The UserControl defines the event handlers for the GridView's events. The code roughly looks as follows:

private DataTable dataTable = new DataTable();
protected SPGridView grid;

protected override void OnLoad(EventArgs e)
{
    DoDataBind(); // Creates the grid. This is essential in order for postback events to work.
}

protected override void Render(HtmlTextWriter writer)
{
    DoDataBind();
    base.Render(writer); // Renews the grid according to the latest changes
}

void ReadButton_Command(object sender, CommandEventArgs e)
{
    ImageButton button = (ImageButton)sender;
    GridViewRow viewRow = (GridViewRow)button.NamingContainer;
    int rowIndex = viewRow.RowIndex;

    // rowIndex is used to identify the row in which the button was clicked,
    // since the control.ID is equal for all rows.
    // [... some code to process the event ...]
}

private void DoDataBind()
{
    // [... Some code to fill the dataTable ...]

    grid.AutoGenerateColumns = false;

    grid.Columns.Clear();

    TemplateField templateField = new TemplateField();
    templateField.HeaderText = "";
    templateField.ItemTemplate = new MyItemTemplate(new CommandEventHandler(ReadButton_Command));
    grid.Columns.Add(templateField);

    grid.DataSource = this.dataTable.DefaultView;
    grid.DataBind();
}

private class MyItemTemplate : ITemplate
{
    private CommandEventHandler commandEventHandler;

    public MyItemTemplate(CommandEventHandler commandEventHandler)
    {
        this.commandEventHandler = commandEventHandler;
    }

    public void InstantiateIn(Control container)
    {
        ImageButton imageButton = new ImageButton();
        imageButton.ID = "btnRead";
        imageButton.Command += commandEventHandler;
        imageButton.DataBinding += new EventHandler(imageButton_DataBinding);
        container.Controls.Add(imageButton);
    }

    void imageButton_DataBinding(object sender, EventArgs e)
    {
        // Code to get image URL
    }
}

只是重复:在每一个生命周期,第一个在onload被执行,其生成与ImageButtons电网。然后,事件被处理。由于按键都在那里,这些事件通常工作。随后,渲染被调用,产生从基于新的数据的临时电网。这始终工作,除了第一次用户点击图像按钮,虽然我已经断言,当页面发送给用户的第一次,也产生了网格和图像按钮。

Just to repeat: At each lifecycle, first the OnLoad is executed, which generates the Grid with the ImageButtons. Then, the events are processed. Since the buttons are there, the events usually work. Afterwards, Render is called, which generates the Grid from scratch based upon the new data. This always works, except for the very first time the user clicks on an image button, although I have asserted that the grid and image buttons are also generated when the page is sent to the user for the first time.

希望有人能帮助我理解这一点,或告诉我,我的情况更好的解决方案。

Hope that someone can help me understand this or tell me a better solution for my situation.

推荐答案

由于在我看来这是一个局部的答案,我重新张贴这种方式:

Since in my opinion this is a partial answer, I re-post it this way:


  • 如果我用正常的按钮,而不是ImageButtons(在相同的位置,即仍采用MyItemTemplate但实例化按钮而不是在的ImageButtonInstantiateIn,它工作正常。

  • If I use normal Buttons instead of ImageButtons (in the exact same place, i.e. still using MyItemTemplate but instantiating Button instead of ImageButton in "InstantiateIn", it works fine.

如果我断言,DoDataBind()的内容发送到客户端之前总是执行两次,它工作正常ImageButtons。

If I assert that DoDataBind() is always executed twice before sending the content to the client, it works fine with ImageButtons.

不过百思不得其解,但无论...

Still puzzled, but whatever...

这篇关于ASP.NET:处理回发而不被解雇的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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