Thursday, September 20, 2012

ASP.NET Validate In place edit GridView using RequiredFieldValidator.

ASP.NET Validate In place edit GridView using RequiredFieldValidator.

This very easy demo will show you how to validate TextBox in GridView by using RequiredFieldValidator.
First we must add TextBox in EditItemTemplate for display after click edit.
Second is fire event for update data.
Demo Code show below.

The Result

 CustomerIDCustomerName
Update Cancel 0 *
Edit 1 Dow jones
Edit 2 Million Dollar
Edit 3 Bank of America

Demo Code

Code HTML

        <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" 
            EnableModelValidation="True" 
            DataKeyNames="CustomerID"
            onrowediting="gv1_RowEditing" 
            onrowupdating="gv1_RowUpdating" 
            onrowcancelingedit="gv1_RowCancelingEdit"
            >
        <Columns>
            <asp:CommandField ShowEditButton="True" ValidationGroup="Update" />
            <asp:TemplateField HeaderText="CustomerID">
                <ItemTemplate>
                    <asp:Label ID="lblCustomerID" runat="server" Text='<%# Bind("CustomerID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CustomerName">
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("CustomerID") %>'></asp:Label>
                </ItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCustomer" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCustomerName" runat="server" MaxLength="200" Text='<%# Bind("CustomerName") %>' ValidationGroup="Update" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqtxtCustomerName" runat="server" Text="*" Display="Static" ControlToValidate="txtCustomerName" ValidationGroup="Update" ></asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>
    

Code Behind

    private DataTable CustomerData
    {
        get {
            if (ViewState["CustomerData"] != null)
                return (DataTable)ViewState["CustomerData"];

            //Declare DataTable
            DataTable dt = new DataTable("TableName1");
            //Add Column
            dt.Columns.Add("CustomerID");
            dt.Columns.Add("CustomerName");

            //Declare Datarow
            DataRow dr = null;
            dr = dt.NewRow();
            dr["CustomerID"] = "0";
            dr["CustomerName"] = "Adam J";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["CustomerID"] = "1";
            dr["CustomerName"] = "Dow jones";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["CustomerID"] = "2";
            dr["CustomerName"] = "Million Dollar";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["CustomerID"] = "3";
            dr["CustomerName"] = "Bank of America";
            dt.Rows.Add(dr);

            ViewState["CustomerData"] = dt;
            return dt;
        }
        set {
            ViewState["CustomerData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }
    protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtCustomerName = (TextBox)gv1.Rows[e.RowIndex].FindControl("txtCustomerName");
        Label lblCustomerID = (Label)gv1.Rows[e.RowIndex].FindControl("lblCustomerID");
        foreach (DataRow dr in CustomerData.Select("CustomerID='" + lblCustomerID.Text + "'"))
        {
            dr["CustomerName"] = txtCustomerName.Text;
        }
        CustomerData.AcceptChanges();
        gv1.EditIndex = -1;
        BindData();
    }
    protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv1.EditIndex = -1;
        BindData();
    }
    void BindData()
    {
        gv1.DataSource = CustomerData;
        gv1.DataBind();
    }
    

Don't hesitate to contact me zomdev.

No comments:

Post a Comment