自动生成TemplateField,包括验证控件,比如非空,输入格式,数据范围等根据配置信息自动生成,下面是原代码,在验证控件方面还没处理好。想用CustomValidator,可就是不能触发事件,如直接添加在页面上则正常,不知何解。
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 System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Specialized;
/// <summary>
/// DetailsViewTemplate DetailsView生成模板
/// </summary>
public class DetailsViewTemplate : ITemplate //DetailsView生成模板
{
private DataControlRowType templateType; //行类型
private string ColumnName, ColumnDesc, DataTpye, DataLength, ControlType;//ColumnName列名,ColumnDesc列说明,DataTpye数据类型,DataLength长度,ControlType控件类型
private DataTable DT, Table; //DT设置信息列表,Table下拉值
public DetailsViewTemplate(DataControlRowType type, DataTable dt,string strControlType, DataTable strDataTable)
{
templateType = type;
DT = dt;
ControlType = strControlType; //控件类型
Table = strDataTable; //DropDownList绑定值
}
[DebuggerNonUserCode]
public void InstantiateIn(System.Web.UI.Control container)
{
//Create the content for the different row types.
ColumnName = DT.Rows[0]["ColumnName"].ToString();
ColumnDesc = DT.Rows[0]["ColumnDesc"].ToString();
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
if ((bool)DT.Rows[0]["NullNO"] == false)
{
lc.Text = "*" + ColumnDesc + ":";
}
else
{
lc.Text = ColumnDesc + ":";
}
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
switch (ControlType)
{
case "Label":
//生成Label控件
Label L = new Label();
L.ID = "lab" + ColumnName;
L.DataBinding += new EventHandler(this.Label_DataBinding);
container.Controls.Add(L);
break;
case "TextBox":
//生成TextBox控件
DataTpye = DT.Rows[0]["Tpye"].ToString();
DataLength = DT.Rows[0]["Length"].ToString();
TextBox TB = new TextBox();
TB.ID = "txt" + ColumnName;
switch (DataTpye)
{
case "decimal":
break;
case "int":
break;
case "varchar":
//TB.MaxLength = Convert.ToInt32(DataLength);
break;
}
TB.DataBinding += new EventHandler(this.TextBox_DataBinding);
container.Controls.Add(TB);
//生成验证控件
if ((bool)DT.Rows[0]["NullNO"] == false)
{
RequiredFieldValidator RFV = new RequiredFieldValidator();
RFV.ID = "rfv" + ColumnName;
RFV.ErrorMessage = "请输入" + ColumnDesc;
RFV.ControlToValidate = "txt" + ColumnName;
container.Controls.Add(RFV);
}
switch (DataTpye)
{
case "decimal":
RegularExpressionValidator REV = new RegularExpressionValidator();
REV.ID = "rev" + ColumnName;
REV.ControlToValidate = "txt" + ColumnName;
REV.ErrorMessage = "只能输入数字;";
REV.ValidationExpression = @"\d+(\.\d+)?$";
container.Controls.Add(REV);
break;
case "int":
RegularExpressionValidator REV1 = new RegularExpressionValidator();
REV1.ID = "rev" + ColumnName;
REV1.ControlToValidate = "txt" + ColumnName;
REV1.ErrorMessage = "只能输入整字;";
REV1.ValidationExpression = @"\d*";
container.Controls.Add(REV1);
break;
}
break;
case "DropDownList":
//生成DropDownList控件
DropDownList DDL = new DropDownList();
DDL.ID = "ddl" + ColumnName;
DDL.DataBinding += new EventHandler(this.DropDownList_DataBinding);
container.Controls.Add(DDL);
//生成验证控件
if ((bool)DT.Rows[0]["NullNO"] == false)
{
RequiredFieldValidator RFV1 = new RequiredFieldValidator();
RFV1.ID = "rfv" + ColumnName;
RFV1.ErrorMessage = "请选择" + ColumnDesc;
RFV1.ControlToValidate = "ddl" + ColumnName;
container.Controls.Add(RFV1);
}
break;
default:
break;
}
break;
default:
// Insert code to handle unexpected values.
break;
}
}
[DebuggerNonUserCode]
private void TextBox_DataBinding(Object sender, EventArgs e) //生成TextBox
{
TextBox box = (TextBox)sender;
//box.TemplateControl =;
IDataItemContainer bindingContainer = (IDataItemContainer)box.BindingContainer;
if (box.Page.GetDataItem() != null)
{
box.Text = Convert.ToString(DataBinder.Eval(box.Page.GetDataItem(), ColumnName), CultureInfo.CurrentCulture);
}
}
[DebuggerNonUserCode]
private void Label_DataBinding(Object sender, EventArgs e) //生成Label
{
Label L = (Label)sender;
IDataItemContainer bindingContainer = (IDataItemContainer)L.BindingContainer;
if (L.Page.GetDataItem() != null)
{
L.Text = Convert.ToString(DataBinder.Eval(L.Page.GetDataItem(), ColumnName), CultureInfo.CurrentCulture);
}
}
[DebuggerNonUserCode]
private void DropDownList_DataBinding(Object sender, EventArgs e) //生成DropDownList
{
DropDownList DDL = (DropDownList)sender;
IDataItemContainer bindingContainer = (IDataItemContainer)DDL.BindingContainer;
DDL.AppendDataBoundItems = true;
//DDL.Style.Add("width", "200px");
DDL.DataSource = Table;
DDL.DataTextField = "ColumnDesc";
DDL.DataValueField = "ColumnValue";
DDL.Items.Add(new ListItem("无", ""));
if (DDL.Page.GetDataItem() != null)
{
DDL.SelectedValue = Convert.ToString(DataBinder.Eval(DDL.Page.GetDataItem(), ColumnName), CultureInfo.CurrentCulture);
}
}
}