How to enable in-place editing in an asp:GridView?

前端 未结 2 408
误落风尘
误落风尘 2021-01-05 18:19

How can i add edit boxes, and read their values during submit, with an asp:Repeater?


i have an asp:GridView which is displaying a rea

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 19:07

    Have you tried by setting up the EditIndex property of the DataGrid?

    Example:

    
        
            
        
    
    

    Code behind

        protected void grdProducts_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.grdProducts.EditIndex = e.NewEditIndex;
            This.BindGrid();
        }
    

    Note that you have to re-bind your grid

    Usually you save the data per row, which means, you have an edit link in each row and after you enter edit mode, a save button and optionally a cancel button appear which will allow you to save the values of that specific row

    Following this approach is trivial when using the GridView:

        protected void grdProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // old values for the current row
            var oldValues = e.OldValues;
    
            // new (updated) values for the current row
            var newvalues = e.NewValues;
    
            // Exit edit mode
            this.grdProducts.EditIndex = -1;
    
            // Update the grid
            this.BindGrid();
        }
    

    In the grid markup just add the following:

        onrowupdating="grdProducts_RowUpdating"
    

    If you need to specify custom controls when editing or when displaying the cell data in read-only mode, use grid templates:

           
            
                
                    
                
                
                    
                
            
          
    

提交回复
热议问题