Thursday, September 15, 2011

GridView Validation on edit update action

Use below JavaScript function in .aspx

function ValidateGrid(e)
        {               
            var cnt = 0;
            var grid = document.getElementById('<%= gvActionItems.ClientID %>');
            for(i=1;i<grid.rows.length;i++)
                {               
                    if(grid.rows[i].cells[4].children[0].id == e.id)
                    {                       
                        if(grid.rows[i].cells[0].children[0].value == "")
                        {
                            alert("Please add Action Item");
                            cnt = 1;
                            return false;
                        }
                        if(grid.rows[i].cells[3].children[0].value == "")
                        {
                            alert("Please enter a Target Date");
                            cnt = 1;
                            return false;
                        }                     
                    }                 
                }
            if(cnt > 0)
                return false;
            else
                return true;
     }

In .cs file under GridView's RowDataBound Event put this code
protected void gvActionItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[4].HasControls())
                {
                    LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[4].Controls[0]);
                    lnkbtnDelete.Attributes.Add("id", "LnEdtUpdt");
                    lnkbtnDelete.Attributes.Add("onclick", "return ValidateGrid(this);");
                }
            }
        }

No comments:

Post a Comment