动态创建模板,验证数据问题,请问如何解决,谢谢!

520NET 2013-08-15 11:54:57
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
</asp:GridView>

</div>
</form>
</body>
</html>

代码:

public partial class _Default : System.Web.UI.Page
{
private DataTable getDateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("CODE", typeof(Int32)));
dt.Columns.Add(new DataColumn("NAME", typeof(String)));
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = "01";
dt.Rows.Add(dr);
return dt;
}

protected override void OnInit(EventArgs e)
{
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "CODE");
GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lblCODE", "CODE");
customField.ItemTemplate = gvt;
GridView1.Columns.Add(customField);
base.OnInit(e);
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = getDateTable();
GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Label tb = (Label)e.Row.FindControl("lblCODE");
RegularExpressionValidator regexpvalidator = new RegularExpressionValidator();
regexpvalidator.ID = "RegularExpressionValidator1";
regexpvalidator.ValidationExpression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
regexpvalidator.ControlToValidate = "lblCODE";
regexpvalidator.ErrorMessage = "Invalid Email Id";
regexpvalidator.SetFocusOnError = true;
e.Row.Cells[0].Controls.Add(regexpvalidator);
}
}
}

public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string ID;

public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}

public GridViewTemplate(DataControlRowType type, string controlID, string colname)
{
templateType = type;
ID = controlID;
columnName = colname;
}

public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
Label lbl = new Label();
lbl.ID = ID;
lbl.DataBinding += new System.EventHandler(TextBox_DataBinding);
container.Controls.Add(lbl);
break;
default:
break;
}
}
private void TextBox_DataBinding(object sender, EventArgs e)
{
Label lbl = sender as Label;
if (lbl != null)
{
GridViewRow container = lbl.NamingContainer as GridViewRow;
if (container != null)
{
object dataValue = DataBinder.Eval(container.DataItem, columnName);
if (dataValue != DBNull.Value)
{
lbl.Text = dataValue.ToString();
}
}
}
}

}
...全文
97 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Robin 2013-08-24
  • 打赏
  • 举报
回复
很明显不能验证label控件啊。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧