how to validate controls with in gridview using validation controls?
-
Monday, March 05, 2012 5:54 PM
Code for Default.aspx
=============================
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebTest._Default" %><asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<div>
<asp:GridView ID="gvCountryDetail" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="CountryId"
DataSourceID="sdCountry" onrowcommand="gvCountryDetail_RowCommand"
onrowcreated="gvCountryDetail_RowCreated"
onrowdatabound="gvCountryDetail_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Panel ID="pnlCountry" runat="server" Visible="false">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
Country Code
</td>
<td>
<asp:TextBox ID="txtCountryCode" runat="server" Text='<%# Bind("CountryCode") %>' CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCountryCode" runat="server" ErrorMessage="RequiredFieldValidator"
ControlToValidate="txtCountryCode" ValidationGroup="EditValidationControls" CausesValidation="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Country Name
</td>
<td>
<asp:TextBox ID="txtCountryName" runat="server" Text='<%# Bind("CountryName") %>' CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCountryName" runat="server" ErrorMessage="RequiredFieldValidator"
ControlToValidate="txtCountryName" ValidationGroup="EditValidationControls" CausesValidation="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lbtnUpdate" runat="server" CommandName="Update" ValidationGroup="EditValidationControls" CausesValidation="true">Update</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lbtnCancel" runat="server" CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
</td>
</tr>
</table>
</asp:Panel>
</EditItemTemplate>
<ItemTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Country Code</td>
<td>
<asp:Label ID="lblCountryCode" runat="server" Text='<%# Bind("CountryCode") %>'></asp:Label>
</td>
</tr>
<tr>
<td>Country Name</td>
<td>
<asp:Label ID="lblCountryName" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" CausesValidation="false">Edit</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ValidationSummary ID="vsEditValidationControls" runat="server" ShowSummary="false" ValidationGroup="EditValidationControls"/>
<asp:SqlDataSource ID="sdCountry" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Country]"
InsertCommand="INSERT INTO Country(CountryCode, CountryName) VALUES (@CountryCode, @CountryName)"
UpdateCommand="UPDATE Country SET CountryCode = @CountryCode, CountryName = @CountryName WHERE (CountryId = @CountryId)">
<InsertParameters>
<asp:Parameter Name="CountryCode" />
<asp:Parameter Name="CountryName" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CountryCode" />
<asp:Parameter Name="CountryName" />
<asp:Parameter Name="CountryId" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</asp:Content>
===================================================
Code for Default.aspx.cs
===================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void gvCountryDetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);switch (e.CommandName.ToLower())
{
case "update":
Validate("EditValidationControls");
if (Page.IsValid)
return;
ViewState["EditMode"] = "update";
gvCountryDetail.EditIndex = -1;
break;
case "cancel":
ViewState["EditMode"] = "cancel";
gvCountryDetail.EditIndex = -1;
break;
case "edit":
gvCountryDetail.EditIndex = index;
ViewState["EditMode"] = "edit";
break;
}
}protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow row in gvCountryDetail.Rows)
{
if (row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || (row.RowState == DataControlRowState.Edit))
{
string EditMode = ViewState["EditMode"].ToString();
Panel pl;switch (EditMode.ToLower())
{
case "edit":
pl = (Panel)row.FindControl("pnlCountry");
pl.Visible = true;
break;
case "cancel":
pl = (Panel)row.FindControl("pnlCountry");
pl.Visible = false;
break;
case "update":
pl = (Panel)row.FindControl("pnlCountry");
pl.Visible = false;
break;
}
}
}base.Render(writer);
}protected void gvCountryDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow) && (gvCountryDetail.EditIndex != e.Row.RowIndex))
{
LinkButton lbtnEdit = (LinkButton)e.Row.FindControl("lbtnEdit");
lbtnEdit.CommandArgument = e.Row.RowIndex.ToString();
}
}protected void gvCountryDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || (e.Row.RowState == DataControlRowState.Edit))
{
LinkButton lbtnUpdate = (LinkButton)e.Row.FindControl("lbtnUpdate");
lbtnUpdate.CommandArgument = e.Row.RowIndex.ToString();
LinkButton lbtnCancel = (LinkButton)e.Row.FindControl("lbtnCancel");
lbtnCancel.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
}
=======================================================
I need to validate the values for the non empty values entered in the texbox field after clicking on update button.
please help....
All Replies
-
Wednesday, March 07, 2012 4:52 AM
If anyone solve this issue, please post the solution here.Code for Default.aspx
=============================
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebTest._Default" %><asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<div>
<asp:GridView ID="gvCountryDetail" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="CountryId"
DataSourceID="sdCountry" onrowcommand="gvCountryDetail_RowCommand"
onrowcreated="gvCountryDetail_RowCreated"
onrowdatabound="gvCountryDetail_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Panel ID="pnlCountry" runat="server" Visible="false">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
Country Code
</td>
<td>
<asp:TextBox ID="txtCountryCode" runat="server" Text='<%# Bind("CountryCode") %>' CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCountryCode" runat="server" ErrorMessage="RequiredFieldValidator"
ControlToValidate="txtCountryCode" ValidationGroup="EditValidationControls" CausesValidation="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Country Name
</td>
<td>
<asp:TextBox ID="txtCountryName" runat="server" Text='<%# Bind("CountryName") %>' CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCountryName" runat="server" ErrorMessage="RequiredFieldValidator"
ControlToValidate="txtCountryName" ValidationGroup="EditValidationControls" CausesValidation="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lbtnUpdate" runat="server" CommandName="Update" ValidationGroup="EditValidationControls" CausesValidation="true">Update</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lbtnCancel" runat="server" CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
</td>
</tr>
</table>
</asp:Panel>
</EditItemTemplate>
<ItemTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Country Code</td>
<td>
<asp:Label ID="lblCountryCode" runat="server" Text='<%# Bind("CountryCode") %>'></asp:Label>
</td>
</tr>
<tr>
<td>Country Name</td>
<td>
<asp:Label ID="lblCountryName" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" CausesValidation="false">Edit</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ValidationSummary ID="vsEditValidationControls" runat="server" ShowSummary="false" ValidationGroup="EditValidationControls"/>
<asp:SqlDataSource ID="sdCountry" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Country]"
InsertCommand="INSERT INTO Country(CountryCode, CountryName) VALUES (@CountryCode, @CountryName)"
UpdateCommand="UPDATE Country SET CountryCode = @CountryCode, CountryName = @CountryName WHERE (CountryId = @CountryId)">
<InsertParameters>
<asp:Parameter Name="CountryCode" />
<asp:Parameter Name="CountryName" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CountryCode" />
<asp:Parameter Name="CountryName" />
<asp:Parameter Name="CountryId" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</asp:Content>
===================================================
Code for Default.aspx.cs
===================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void gvCountryDetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);switch (e.CommandName.ToLower())
{
case "update":
Validate("EditValidationControls");
if (Page.IsValid)
return;
ViewState["EditMode"] = "update";
gvCountryDetail.EditIndex = -1;
break;
case "cancel":
ViewState["EditMode"] = "cancel";
gvCountryDetail.EditIndex = -1;
break;
case "edit":
gvCountryDetail.EditIndex = index;
ViewState["EditMode"] = "edit";
break;
}
}protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow row in gvCountryDetail.Rows)
{
if (row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || (row.RowState == DataControlRowState.Edit))
{
string EditMode = ViewState["EditMode"].ToString();
Panel pl;switch (EditMode.ToLower())
{
case "edit":
pl = (Panel)row.FindControl("pnlCountry");
pl.Visible = true;
break;
case "cancel":
pl = (Panel)row.FindControl("pnlCountry");
pl.Visible = false;
break;
case "update":
pl = (Panel)row.FindControl("pnlCountry");
pl.Visible = false;
break;
}
}
}base.Render(writer);
}protected void gvCountryDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow) && (gvCountryDetail.EditIndex != e.Row.RowIndex))
{
LinkButton lbtnEdit = (LinkButton)e.Row.FindControl("lbtnEdit");
lbtnEdit.CommandArgument = e.Row.RowIndex.ToString();
}
}protected void gvCountryDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || (e.Row.RowState == DataControlRowState.Edit))
{
LinkButton lbtnUpdate = (LinkButton)e.Row.FindControl("lbtnUpdate");
lbtnUpdate.CommandArgument = e.Row.RowIndex.ToString();
LinkButton lbtnCancel = (LinkButton)e.Row.FindControl("lbtnCancel");
lbtnCancel.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
}
=======================================================
I need to validate the values for the non empty values entered in the texbox field after clicking on update button.
please help....
-
Thursday, March 08, 2012 6:22 AMModerator
Hi,
Please post asp.net issues in asp.net forums, the experts there could help you in a more efficient way.
Thanks.
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com. Microsoft One Code Framework
- Marked As Answer by Lloyd ZhangMicrosoft Contingent Staff, Moderator Monday, March 12, 2012 2:02 AM

