2012-07-05 49 views
0

我有一個自定義的GridView控件,我從數據庫中抓取數據以填充控件。在網頁上我還創建了一個HeaderTemplate中CheckBox控件和一個ItemTemplate CheckBox控件:Custom Control GridView Control中的複選框事件

<nm:ContactGridViewControl runat="server" ID="grdContacts"> 
     <Columns> 
      <asp:TemplateField> 
       <HeaderTemplate> 
        <asp:CheckBox runat="server" AutoPostBack="true" /> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <asp:CheckBox runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </nm:ContactGridViewControl> 

我填充的GridView如在OnInit事件如下。我選擇不重新填充每一個回傳,因爲它會減慢應用程序。

protected override void OnInit(EventArgs e) 
    { 
     this.RowDataBound += new GridViewRowEventHandler(ContactGridViewControl_RowDataBound); 
     this.RowCreated += new GridViewRowEventHandler(ContactGridViewControl_RowCreated); 

     if (!Page.IsPostBack) 
     { 
      List<EnquiryItem> contactList = new List<EnquiryItem>(); 
      DataTable list = new DataTable(); 

      if (SessionManager.LoginState != null) 
      { 
       contactList = SiteDataLayerHandler.GetContactList(SessionManager.LoginState.UserID); 
      } 

      if (contactList != null) 
      { 
       list.Columns.Add("LeadID"); 
       list.Columns.Add("Name"); 
       list.Columns.Add("Email Address"); 

       foreach (EnquiryItem item in contactList) 
       { 
        DataRow row = list.NewRow(); 

        row["LeadID"] = item.LeadID; 
        row["Name"] = string.Format("{0} {1}", item.FirstName.ToCapitalize(), item.LastName.ToCapitalize()); 
        row["Email Address"] = item.EmailAddress; 

        list.Rows.Add(row); 

       } 

       this.DataSource = list; 
       this.DataBind(); 
      } 
     } 

     base.OnInit(e); 
    } 

爲了保持與我添加了一個「的CheckedChanged」事件有關的動態在一個地方控制相關聯的所有代碼「OnRowDataBound」這僅僅是在HeaderTemplate中的複選框。原因是這樣我就可以使用這個選項框爲「選擇/取消所有行」:

protected void ContactGridViewControl_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowIndex == -1) // Check if row is Header row 
     { 
      CheckBox chk = e.Row.GetAllControls().OfType<CheckBox>().FirstOrDefault(); 

      if (chk != null) 
      { 
       chk.CheckedChanged += new EventHandler(chk_CheckedChanged); 
      } 
     } 
    } 

我那麼有事件代碼在同一頁上,像這樣:

protected void chk_CheckedChanged(object sender, EventArgs e) 
    { 
     bool isChecked = ((CheckBox)sender).Checked; 

     foreach (GridViewRow row in this.Rows) 
     { 
      CheckBox chkBox = row.Cells[0].Controls[0] as CheckBox; 

      if (chkBox != null) 
      { 
       chkBox.Checked = isChecked; 
      } 
     } 
    } 

這就是問題開始。我的事件永遠不會被擊中!但是,該複選框會回發。

回答

0

好吧,所以答案是這樣的。我需要的,而不是「OnRowDataBound」

protected override void OnRowCreated(GridViewRowEventArgs e) 
    { 
     if (e.Row.RowIndex == -1) 
     { 
      CheckBox chk = e.Row.GetAllControls().OfType<CheckBox>().FirstOrDefault(); 

      if (chk != null) 
      { 
       chk.CheckedChanged += new EventHandler(chk_CheckedChanged); 
      } 
     } 
     base.OnRowCreated(e); 
    } 

這樣的「OnRowCreated」事件的事件撞擊方法每次

+1

哪裏GetAllControls擴展方法來自哪裏分配的CheckedChanged事件? – Zack 2013-07-15 20:18:29