2010-07-18 2 views

답변

3

서브 클래스에게 ... 등 업데이 트를 삭제하는 GridView 컨트롤을 적용하고 이상을 CreateChildControls 방법을 타는 것

protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 
    { 
     // re-use the AutoGenerate...Button properties 
     bool showDelete = AutoGenerateDeleteButton; 
     bool showEdit = AutoGenerateEditButton; 
     bool showSelect = AutoGenerateSelectButton; 

     // turn them all off, we'll be creating our own 
     AutoGenerateDeleteButton = false; 
     AutoGenerateEditButton = false; 
     AutoGenerateSelectButton = false; 

     // hide the column if it already exists 
     if (Columns[0].GetType() == typeof(CommandField)) 
     { 
      Columns.RemoveAt(0); 
     } 

     // add the command column if necessary 
     if (showDelete || showEdit || showSelect) 
     { 
      CommandField cmdField = new CommandField(); 
      cmdField.HeaderText = string.Empty; 
      cmdField.ButtonType = ButtonType.Image; 
      cmdField.ShowSelectButton = showSelect; 
      cmdField.ShowEditButton = showEdit; 
      cmdField.ShowDeleteButton = showDelete; 
      cmdField.DeleteImageUrl = "~/images/delete.bmp"; 
      cmdField.EditImageUrl = "~/images/edit.bmp"; 
      cmdField.SelectImageUrl = "~/images/select.bmp"; 

      Columns.Insert(0, cmdField); 
     } 

     // this will show the grid even if there is no data 
     int numRows = base.CreateChildControls(dataSource, dataBinding); 

     //no data rows created, create empty table if enabled 
     if (numRows == 0 && ShowWhenEmpty) 
     { 
      //create table 
      Table table = new Table(); 
      table.ID = this.ID; 

      //convert the exisiting columns into an array and initialize 
      DataControlField[] fields = new DataControlField[this.Columns.Count]; 
      this.Columns.CopyTo(fields, 0); 

      if (this.ShowHeader) 
      { 
       //create a new header row 
       _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); 

       this.InitializeRow(_headerRow2, fields); 
       table.Rows.Add(_headerRow2); 
      } 

      //create the empty row 
      GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); 

      TableCell cell = new TableCell(); 
      cell.ColumnSpan = this.Columns.Count; 
      cell.Width = Unit.Percentage(100); 
      if (!String.IsNullOrEmpty(EmptyDataText)) 
       cell.Controls.Add(new LiteralControl(EmptyDataText)); 

      if (this.EmptyDataTemplate != null) 
       EmptyDataTemplate.InstantiateIn(cell); 

      emptyRow.Cells.Add(cell); 
      table.Rows.Add(emptyRow); 

      if (this.ShowFooter) 
      { 
       //create footer row 
       _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); 

       this.InitializeRow(_footerRow2, fields); 
       table.Rows.Add(_footerRow2); 
      } 

      this.Controls.Clear(); 
      this.Controls.Add(table); 
     } 

     // I wanted one place to set alternating color for all instances of this control 
     base.AlternatingRowStyle.BackColor = System.Drawing.Color.LightBlue; 

     // now that the controls have been created, it's safe to reset these to their original values. They'll be needed if you bind data later 
     AutoGenerateDeleteButton = showDelete; 
     AutoGenerateEditButton = showEdit; 
     AutoGenerateSelectButton = showSelect; 

     return numRows; 
    } 
1

GridView.RowDataBound 이벤트와 연결할 수 있습니다. 그에서 FindControl이 자동 생성 버튼을 사용자 정의 할 사용

protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     LinkButton link = e.Row.Cells[0].Controls[0] as LinkButton; 
     // do your stuff 
    } 
} 
4

이 작업을 수행하는 가장 쉬운 방법은 모든 것을 스스로 처리하는 것입니다. 뒤에 코드에 대한 지금

<asp:GridView ID="yourGrid" runat="server" OnRowEditing="yourGrid_RowEditing"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:ImageButton ID="yourEditButton" runat="server" 
        CommandName="Edit" ImageUrl="edit.jpg" /> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <!-- your edit controls here --> 
      </EditItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

:

protected void yourGrid_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    // You could just do yourGrid and ignore casting the sender but this 
    // makes the code generic for reuse. 
    GridView grid = (GridView)sender; 
    grid.EditIndex = e.NewEditIndex; 
    BindData(); // need to rebind once the edit index is set. 
} 

이 거의 ImageButton으로 자동 생성 된 편집 버튼을 대체 여기에 편집 명령 단추를 대체하는 ImageButton를 사용하여 빠른 예입니다. CommandName을 편집하도록 설정하면 자동 생성 편집 버튼과 완전히 동일한 이벤트가 트리거됩니다. 이것은 또한

관련 문제