Xpode.com        Click here to Print this article.

Validation on Textbox in Gridview

This tutorial will tell you how to make validations on Textbox in GridView TemplateColumn. To validate control we will place validators in EditItemTemplate of GridView (In HTML Side)

Required field validator check on the textbox in gridview

< asp:TemplateColumn HeaderText="Title">
   < ItemTemplate>
       < asp:Label ID="lblAge" runat="server"  
      Text='<%#DataBinder.Eval(Container.DataItem,"Age")%>'>
      < /asp:Label> < /ItemTemplate>    < EditItemTemplate> < asp:TextBox ID="txtAge" runat="server"     Text='<%#DataBinder.Eval(Container.DataItem,"Age")%>'>
      < /asp:TextBox> < asp:RequiredFieldValidator ID="rw" runat="Server"
      ControlToValidate="txtAge" Display="dynamic"
      ErrorMessage="Insert Age" ValidationGroup="a">
      < /asp:RequiredFieldValidator> < asp:ValidationSummary DisplayMode="BulletList"
      ShowSummary="false" ShowMessageBox="true" ID="ValidationSummary1" EnableClientScript="false"
      ValidationGroup="a" runat="server" /> < EditItemTemplate>

Numeric check on the textbox in gridview

If You want to check the datatype the you may write the below code also. With below code only integer values will be allowed to enter in age field

< asp:TemplateColumn HeaderText="Title">
      < ItemTemplate>
       < asp:Label ID="lblAge" runat="server" 
         Text='<%#DataBinder.Eval(Container.DataItem,"Age")%>'>
       < /asp:Label> < /ItemTemplate> < EditItemTemplate> < asp:TextBox ID="txtAge" runat="server"
         Text='<%#DataBinder.Eval(Container.DataItem,"Age")%>'>       
      < asp:RequiredFieldValidator ID="rw" runat="Server"
      ControlToValidate="txtAge" Display="dynamic"
         ErrorMessage="Insert Age" ValidationGroup="a">
         < /asp:RequiredFieldValidator>       < asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtAge"
         Type="Integer" ValidationGroup="a" runat="server"
         ErrorMessage="Only numeric value is allowed"
      Operator="datatypecheck" >< /asp:CompareValidator>       < asp:ValidationSummary DisplayMode="BulletList"
         ShowSummary="false" ShowMessageBox="true"     ID="ValidationSummary1" EnableClientScript="false"
         ValidationGroup="a" runat="server" /> < /EditItemTemplate> < /asp:TemplateColumn>


http://
http://

Contributed by:
Rohit kakria
I am software developer

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=278

Click here to go on website