当前位置:编程学习 > C#/ASP.NET >>

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

页面:
<%@ 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();
                }
            }
        }
    }

}
--------------------编程问答-------------------- 很明显不能验证label控件啊。 --------------------编程问答-------------------- 除
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,