事件连线2次

时间:2010-01-21 10:25:06

标签: c# asp.net events gridview

我有一个事件的问题,如Page_Init,Page_Load,当我点击页面内的Gridview时,有两次连线。

我已检查过AutoEventWireup,但aspx和ascx是正确的(false和OnInit覆盖)。

Gridview中的事件在Page_Init中设置,但如果它在Page_Load中则相同。我对2个Gridviews使用相同的事件。

有些想法可以帮到我吗?

这是我的代码示例

    protected override void OnInit(EventArgs e)
    {
        instance = (IServiceActivity)InterfaceRepository.GetService(typeof(ServiceActivity));

        this.Load += new EventHandler(Page_Load);
        this.CollectionGridView1.RowDeleting += new GridViewDeleteEventHandler(CollectionGridView1_RowDeleting);
        this.CollectionGridView1.RowDataBound += new GridViewRowEventHandler(CollectionGridView1_RowDataBound);
        this.CollectionGridView1.RowEditing += new GridViewEditEventHandler(CollectionGridView1_RowEditing);
        this.CollectionGridView1.Sorting += new GridViewSortEventHandler(CollectionGridView1_Sorting);
        this.CollectionGridView1.PageIndexChanging += new GridViewPageEventHandler(CollectionGridView1_PageIndexChanging);

        this.CollectionGridView2.RowDeleting += new GridViewDeleteEventHandler(CollectionGridView1_RowDeleting);
        this.CollectionGridView2.RowDataBound += new GridViewRowEventHandler(CollectionGridView1_RowDataBound);
        this.CollectionGridView2.RowEditing += new GridViewEditEventHandler(CollectionGridView1_RowEditing);
        this.CollectionGridView2.Sorting += new GridViewSortEventHandler(CollectionGridView1_Sorting);
        this.CollectionGridView2.PageIndexChanging += new GridViewPageEventHandler(CollectionGridView1_PageIndexChanging);

        base.OnInit(e);
    }

   protected void CollectionGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CollectionGridView cgv = (CollectionGridView)sender;
        cgv.SelectedIndex = e.RowIndex;
        CvrActivity cvrAct = new CvrActivity();
        cvrAct = instance.GetActivityById(long.Parse(cgv.SelectedDataKey.Value.ToString()));

        if (cvrAct != null)
        {
            //DELETE        
        }
    }

    protected void CollectionGridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        CollectionGridView cgv = (CollectionGridView)sender;
        cgv.SelectedIndex = e.NewEditIndex;
        base.Modify(long.Parse(cgv.SelectedDataKey.Value.ToString()));
    }

最后,问题在CommandField上,当它在ButtonType =“Image”中时,编辑或删除被触发2次。

那么如何将ButtonType与Image一起使用?

3 个答案:

答案 0 :(得分:0)

评论那些被连线的人,看看会发生什么? : - )

答案 1 :(得分:0)

如果事件有可能已连线,您可以在尝试添加之前将其删除。尝试删除没有的事件处理程序不会导致异常。

// remove existing handler if present
this.CollectionGridView1.RowDeleting -= new GridViewDeleteEventHandle(CollectionGridView1_RowDeleting); 
// add new handler
this.CollectionGridView1.RowDeleting += new GridViewDeleteEventHandle(CollectionGridView1_RowDeleting); 

遵循删除模式,然后添加将确保事件仅处理一次。

答案 2 :(得分:0)

您的网格是否在ajax更新面板中?

我曾经遇到类似的问题,通过删除updatepanel修复了这个问题。