Back to Devexpress

GridViewTemplates.EditForm Property

aspnet-devexpress-dot-web-dot-gridviewtemplates.md

latest12.4 KB
Original Source

GridViewTemplates.EditForm Property

Specifies a template to display an edit form.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
[DefaultValue(null)]
public virtual ITemplate EditForm { get; set; }
vb
<DefaultValue(Nothing)>
Public Overridable Property EditForm As ITemplate

Property Value

TypeDefaultDescription
ITemplatenull

An object that implements the ITemplate interface.

|

Remarks

When you specify the EditForm property, the control creates a template within a container object of the GridViewEditFormTemplateContainer type.

When you use an edit form template, the control does not update the data source automatically. Handle the RowUpdating event to manually update the data source.

Refer to the Create Templates topic for information on how to create templates for the Grid View control’s elements.

Design-Time Example

In the example below, the edit form template contains ASPxPageControl.

Use the ASPxGridViewTemplateReplacement objects to display regular controls within the template.

Run Demo: ASPxGridView - Edit Form Templates

aspx
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="EmployeeID"
    OnRowUpdating="grid_RowUpdating" OnRowInserting="grid_RowInserting">
    <Templates>
        <EditForm>
            <div style="padding: 4px 3px 4px">
                <dx:ASPxPageControl ID="ASPxPageControl1" runat="server" Width="100%">
                    <TabPages>
                        <dx:TabPage Text="Info" Visible="true">
                            <ContentCollection>
                                <dx:ContentControl runat="server">
                                    <dx:ASPxGridViewTemplateReplacement ID="Editors" runat="server"
                                        ReplacementType="EditFormEditors" />
                                </dx:ContentControl>
                            </ContentCollection>
                        </dx:TabPage>
                        <dx:TabPage Text="Notes" Visible="true">
                            <ContentCollection>
                                <dx:ContentControl runat="server">
                                    <dx:ASPxMemo runat="server" ID="notesEditor"
                                        Text='<%# Eval("Notes")%>' Width="100%" Height="93px" />
                                </dx:ContentControl>
                            </ContentCollection>
                        </dx:TabPage>
                    </TabPages>
                </dx:ASPxPageControl>
            </div>
            <div style="text-align: right; padding: 2px">
                <dx:ASPxGridViewTemplateReplacement ID="UpdateButton" runat="server"
                    ReplacementType="EditFormUpdateButton" />
                <dx:ASPxGridViewTemplateReplacement ID="CancelButton" runat="server"
                    ReplacementType="EditFormCancelButton" />
            </div>
        </EditForm>
    </Templates>
</dx:ASPxGridView>
csharp
protected void grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e){
    e.NewValues["Notes"] = GetMemoText();
}

protected void grid_RowInserting(object sender, ASPxDataInsertingEventArgs e){
    e.NewValues["Notes"] = GetMemoText();
}

protected string GetMemoText(){
    ASPxPageControl pageControl = grid.FindEditFormTemplateControl("pageControl") as ASPxPageControl;
    ASPxMemo memo = pageControl.FindControl("notesEditor") as ASPxMemo;
    return memo.Text;
}
vb
Protected Sub grid_RowUpdating(ByVal sender As Object, ByVal e As ASPxDataUpdatingEventArgs)
    e.NewValues("Notes") = GetMemoText()
End Sub

Protected Sub grid_RowInserting(ByVal sender As Object, ByVal e As ASPxDataInsertingEventArgs)
    e.NewValues("Notes") = GetMemoText()
End Sub

Protected Function GetMemoText() As String
    Dim pageControl As ASPxPageControl = TryCast(grid.FindEditFormTemplateControl("pageControl"), ASPxPageControl)
    Dim memo As ASPxMemo = TryCast(pageControl.FindControl("notesEditor"), ASPxMemo)
    Return memo.Text
End Function

Runtime Example

The following example creates an edit form template at runtime:

csharp
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

public partial class _Default : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        ASPxGridView1.Templates.EditForm = new EditFormTemplate();
    }
    protected void ASPxGridView1_RowUpdating(object sender,
    DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
    e.NewValues["ProductName"] = (ASPxGridView1.FindEditFormTemplateControl("tbProductName") 
                as ASPxTextBox).Value;
    e.NewValues["UnitPrice"] = (ASPxGridView1.FindEditFormTemplateControl("spinUnitPrice") 
                 as ASPxSpinEdit).Value;
    }
}

public class EditFormTemplate : ITemplate {
    public void InstantiateIn(Control container) {
        Table table = CreateHtmlTable();
        container.Controls.Add(table);

        ASPxTextBox tb = new ASPxTextBox();
        tb.ID = "tbProductName";
        tb.CssFilePath = @"~/App_Themes/Soft Orange/{0}/styles.css";
        tb.CssPostfix = "Soft_Orange";
        tb.Value = DataBinder.Eval((container as 
              GridViewEditFormTemplateContainer).DataItem, "ProductName");
        table.Rows[0].Cells[0].Controls.Add(tb);

        ASPxSpinEdit spin = new ASPxSpinEdit();
        spin.ID = "spinUnitPrice";
        spin.CssFilePath = @"~/App_Themes/Soft Orange/{0}/styles.css";
        spin.CssPostfix = "Soft_Orange";
        spin.Value = DataBinder.Eval((container as 
             GridViewEditFormTemplateContainer).DataItem, "UnitPrice");
        table.Rows[0].Cells[1].Controls.Add(spin);

        ASPxGridViewTemplateReplacement tr = new ASPxGridViewTemplateReplacement();
        tr.ReplacementType = GridViewTemplateReplacementType.EditFormUpdateButton;
        table.Rows[1].Cells[2].Controls.Add(tr);
        Literal separator = new Literal();
        separator.Text = " | ";
        table.Rows[1].Cells[2].Controls.Add(separator);
        tr = new ASPxGridViewTemplateReplacement();
        tr.ReplacementType = GridViewTemplateReplacementType.EditFormCancelButton;
        table.Rows[1].Cells[2].Controls.Add(tr);
    }

    Table CreateHtmlTable() {
        Table table = new Table();
        table.Rows.Add(new TableRow());
        table.Rows[0].Cells.AddRange(new TableCell[] { new TableCell(),
                                                       new TableCell(),
                                                       new TableCell()});
        table.Rows.Add(new TableRow());
        table.Rows[1].Cells.AddRange(new TableCell[] { new TableCell(),
                                                       new TableCell(),
                                                       new TableCell()});
        return table;
    }
}
vb
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports DevExpress.Web.ASPxEditors
Imports DevExpress.Web.ASPxGridView

Public Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        ASPxGridView1.Templates.EditForm = New EditFormTemplate()
    End Sub
    Protected Sub ASPxGridView1_RowUpdating(sender As Object, e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs)
        e.NewValues("ProductName") = TryCast(ASPxGridView1.FindEditFormTemplateControl("tbProductName"), ASPxTextBox).Value
        e.NewValues("UnitPrice") = TryCast(ASPxGridView1.FindEditFormTemplateControl("spinUnitPrice"), ASPxSpinEdit).Value
    End Sub
End Class

Public Class EditFormTemplate
    Implements ITemplate
    Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
        Dim table As Table = CreateHtmlTable()
        container.Controls.Add(table)

        Dim tb As New ASPxTextBox()
        tb.ID = "tbProductName"
        tb.CssFilePath = "~/App_Themes/Soft Orange/{0}/styles.css"
        tb.CssPostfix = "Soft_Orange"
        tb.Value = DataBinder.Eval(TryCast(container, GridViewEditFormTemplateContainer).DataItem, "ProductName")
        table.Rows(0).Cells(0).Controls.Add(tb)

        Dim spin As New ASPxSpinEdit()
        spin.ID = "spinUnitPrice"
        spin.CssFilePath = "~/App_Themes/Soft Orange/{0}/styles.css"
        spin.CssPostfix = "Soft_Orange"
        spin.Value = DataBinder.Eval(TryCast(container, GridViewEditFormTemplateContainer).DataItem, "UnitPrice")
        table.Rows(0).Cells(1).Controls.Add(spin)

        Dim tr As New ASPxGridViewTemplateReplacement()
        tr.ReplacementType = GridViewTemplateReplacementType.EditFormUpdateButton
        table.Rows(1).Cells(2).Controls.Add(tr)
        Dim separator As New Literal()
        separator.Text = " | "
        table.Rows(1).Cells(2).Controls.Add(separator)
        tr = New ASPxGridViewTemplateReplacement()
        tr.ReplacementType = GridViewTemplateReplacementType.EditFormCancelButton
        table.Rows(1).Cells(2).Controls.Add(tr)
    End Sub

    Private Function CreateHtmlTable() As Table
        Dim table As New Table()
        table.Rows.Add(New TableRow())
        table.Rows(0).Cells.AddRange(New TableCell() {New TableCell(), New TableCell(), New TableCell()})
        table.Rows.Add(New TableRow())
        table.Rows(1).Cells.AddRange(New TableCell() {New TableCell(), New TableCell(), New TableCell()})
        Return table
    End Function
End Class

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the EditForm property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

asp-net-web-forms-grid-initialize-editor-conditionally/CS/WebApplication_Grid/CustomEditForm.aspx.cs#L9

csharp
ASPxGridView grid = (ASPxGridView)sender;
    grid.Templates.EditForm = new CustomEditFormTemplate();
}

asp-net-web-forms-grid-initialize-editor-conditionally/VB/WebApplication_Grid/CustomEditForm.aspx.vb#L11

vb
Dim grid As ASPxGridView = DirectCast(sender, ASPxGridView)
    grid.Templates.EditForm = New CustomEditFormTemplate()
End Sub

See Also

GridViewTemplates Class

GridViewTemplates Members

DevExpress.Web Namespace