开发者

Event wired up 2 times

I have an issue with event as Page_Init, Page_Load, there are wired up 2 times when I click on a Gridview inside my page.

I have checked about AutoEventWireup, but aspx and ascx are correct (false and OnInit overrided).

Event from Gridview are set in Page_Init, but same behavior if it is in Page_Load. I use same event for 2 Gridviews.

Some ideas to help me ?

Here a sample of my code

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

Finally, issues is on CommandField, when it is in ButtonType="Image", Edit or Delete is fired 2 times.

Then how use ButtonType with Image ?


Comment out the ones being wired up and see what happens? :-)


When there is a chance that the event has already been wired up you can remove it before trying to add it. Attempting to remove an event handler where there are none will not cause an exception.

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

Following the pattern of remove then add will ensure that the event is only handled once.


Is your grid inside an ajax update panel?

I once had a similar issue which was fixed by removing the updatepanel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜