DevExpress控件使用交流,DevExpress中国社区 售前咨询
当前位置: 首页 > 开发资源 » 示例代码 » ASPxGridView控件常用示例七:创建编辑(Edit Form)模板

ASPxGridView控件常用示例七:创建编辑(Edit Form)模板

作者:zhuhm   来源:本站原创   浏览:Loading...次   发布时间:2012-11-27   评论:0条

本示例主要展示ASPxGridView网格控件如何在运行时,创建编辑(Edit Form)模板。

C#

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
 


本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [http://www.devexpresscn.com/]
本文地址:http://www.devexpresscn.com/Resources/CodeExamples-326.html

评论列表

暂无评论

请谈谈你的看法 请使用IE或者Firefox浏览器,暂不支持Chrome!

昵称 不填则默认为游客评论

DevExpress DXperience DXv2 v12

DevExpress DXperience 下载 download

DevExpress购买、价格、授权

慧都控件网为DevExpress界面控件的中国地区唯一正式授权经销商,正版控件销售公司,授权代理商,经销商及合作伙伴。

电话:400-700-1020
        023-66090381

邮箱:sales@evget.com

>>如何选择正规控件购买渠道