Skip to main content

How to: Create Edit Form Template at Runtime

  • 2 minutes to read

This example creates the Edit Form‘s template at runtime.

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;
    }
}