2010-01-21 54 views
0

我有一個與Page_Init,Page_Load事件有關的問題,當我點擊我的頁面內的Gridview時,有2次連線。事件有線連接2次

我檢查了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中,當它在按鈕類型=「形象」,編輯或刪除觸發2次。

那麼如何使用ButtonType和Image?

回答

0

註釋掉那些正在連接並看到會發生什麼? :-)

+0

如果我評論,沒有事件發生 – 2010-01-22 09:46:38

0

當有事件發生的可能性時,您可以在嘗試添加事件之前將其刪除。試圖刪除沒有的事件處理程序不會導致異常。

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

按照刪除模式,然後添加將確保該事件只處理一次。

+0

同樣的問題,我看它是不是來自另一頁,加載2次 – 2010-01-22 09:47:30

0

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

我曾經有一個類似的問題,通過刪除updatepanel修復。