GridView needs 2 types of validations as below:
1. FooterRow Validations while Adding
2.EditRow Validations while updating
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Footer Row Validations.
In the below code, GridViewID is gridview id. replace textbox id as per requirment.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<script type="text/javascript">
function Validate_Add() {
var error = "";
var code = document.getElementById('<%=((TextBox)GridViewID.FooterRow.FindControl("txtAddCode")).ClientID%>')
var Server = document.getElementById('<%=((TextBox)GridViewID.FooterRow.FindControl("txtAddServerName")).ClientID%>')
var username = document.getElementById('<%=((TextBox)GridViewID.FooterRow.FindControl("txtAddUserName")).ClientID%>')
var password = document.getElementById('<%=((TextBox)GridViewID.FooterRow.FindControl("txtAddServerPassword")).ClientID%>')
if(code=="")
error+="Enter Unique Code for Server";
if (Server.value == "") {
error += "Enter Server Name\r\n";
}
if (username.value == "")
error += "Enter Server User Name\r\n";
if (password.value == "")
error += "Enter Server User Password\r\n";
if (error == "")
return true;
else {
alert(error)
return false;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Edit Row Validations.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Validate_Edit(btnCtrl) {
var error = "";
var selectedRowIndex = btnCtrl.parentNode.parentNode.rowIndex;
var gridView = document.getElementById('<%= GridViewID.ClientID %>');
var Server = gridView.rows[parseInt(selectedRowIndex)].cells[1].children[0];
var user_name = gridView.rows[parseInt(selectedRowIndex)].cells[2].children[0];
var password = gridView.rows[parseInt(selectedRowIndex)].cells[3].children[0];
if (Server.value == "")
error += "Enter Server Name\r\n";
if (user_name.value == "")
error += "Enter User Name\r\n";
if (password.value == "")
error += "Enter Server User Password\r\n";
if (error == "")
return true;
else {
alert(error)
return false;
}
}
</script>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GridView Code
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<asp:GridView ID="GridViewID" runat="server" CssClass="GridStyle" HorizontalAlign="Left"
AutoGenerateColumns="false" DataKeyNames="Code" AllowPaging="True" PageSize="10"
ShowFooter="true" OnRowCancelingEdit="GridViewID_RowCancelingEdit" OnRowCommand="GridViewID_RowCommand"
OnRowDeleting="GridViewID_RowDeleting" OnRowEditing="GridViewID_RowEditing"
OnRowUpdating="GridViewID_RowUpdating">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Code</HeaderTemplate>
<ItemTemplate>
<%#Eval("Code") %></ItemTemplate>
<EditItemTemplate>
<%#Eval("Code") %>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddCode" runat="server" CssClass="gridcontrol" MaxLength="10"
ToolTip="EnterUnique code for Server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Server Name</HeaderTemplate>
<ItemTemplate>
<%#Eval("ServerName") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditServerName" runat="server" CssClass="gridcontrol" Text='<%#Eval("ServerName") %>'
MaxLength="100" ToolTip="Enter Server Name"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddServerName" runat="server" CssClass="gridcontrol" MaxLength="100"
ToolTip="Enter Server Name"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
User Name</HeaderTemplate>
<ItemTemplate>
<%#Eval("UserName") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditUserName" runat="server" CssClass="gridcontrol" Text='<%#Eval("UserName") %>'
MaxLength="100" ToolTip="Enter Server User Name"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddUserName" runat="server" CssClass="gridcontrol" MaxLength="100"
ToolTip="Enter Server User Name"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Password</HeaderTemplate>
<ItemTemplate>
******</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditPassword" runat="server" CssClass="gridcontrol" TextMode="Password"
MaxLength="100" ToolTip="Enter Server User Passwor"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddServerPassword" runat="server" CssClass="gridcontrol" TextMode="Password"
MaxLength="100" ToolTip="Enter Server User Passwor"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Description</HeaderTemplate>
<ItemTemplate>
<%#Eval("Desc") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditDesc" runat="server" CssClass="gridcontrol" MaxLength="100"
Text='<%#Eval("Desc") %>' ToolTip="Enter Description"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDesc" runat="server" CssClass="gridcontrol" MaxLength="100"
ToolTip="Enter Description"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/Edit.png"
ToolTip="Edit this Server" CssClass="iconstyle" />
<asp:ImageButton ID="imgDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/delete.png"
ToolTip="Delete this Server" CssClass="iconstyle" OnClientClick="javascript:return confirm('Are you sure you want to Delete this Server ?','Yes','No');" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/Update.png"
ToolTip="Update this Server" CssClass="iconstyle" OnClientClick="return Validate_Edit(this)" />
<asp:ImageButton ID="imgDelete" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.png"
ToolTip="Cancel Editing of this Server" CssClass="iconstyle" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" CssClass="btnstyle" ToolTip="Add Server" CommandName="Add"
runat="server" Text="Add" OnClientClick="return Validate_Add();" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle CssClass="GridFooter" />
<HeaderStyle CssClass="GridHeader" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle HorizontalAlign="Center" CssClass="GridPager" />
<AlternatingRowStyle CssClass="GridAlter" />
<RowStyle CssClass="GridRowStyle" />
</asp:GridView>