By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

validation of textbox in gridview

P: 8
hello, i have two textbox column and one button control in gridvie when i click button it will ask plese enter price in first textbox . when enter values it is asking again. how to pass textboxid in javascript
.my code is

Expand|Select|Wrap|Line Numbers
  1.  
  2.  function  validation()
  3.  
  4.  
  5.     {
  6. alert("hi")
  7.  
  8.      var grid = document.getElementById('<%= GridView1.ClientID %>');    
  9.       if(grid!=null)      
  10.  
  11.        {       
  12.        var Inputs = grid.getElementsByTagName("input");         
  13.      for(i = 0; i < Inputs.length; i++)        
  14.  
  15.           {
  16.  
  17.         var grid = document.getElementById('<%= GridView1.ClientID %>');    
  18.         alert(grid)
  19.  
  20.          if(grid!=null) 
  21.              {    
  22.            var Inputs = grid.getElementsByTagName("input");       
  23.              for(i = 0; i < Inputs.length; i++)       
  24.                 {
  25. //                alert(Inputs.length)
  26.                  if((Inputs[i].type == 'text'  ) &&(Inputs[i].id == 'GridView1_ctl03_textbox1'))
  27.                  {
  28.  
  29.  
  30.                   if(Inputs[i].value=="")
  31.                   alert(Inputs[i].value)
  32.                   {
  33.                   //alert(Inputs[i].value)
  34.                   alert("Enter Name ");       
  35.  
  36.  
  37.                             return  false;
  38.  
  39.                   }
  40.  
  41.  
  42.  
  43.                   }
  44.  
  45.                  }
  46.                  }
  47.              } 
  48.           }
  49.         }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. function ValidateText(i) 
  56. {
  57.     if(i.value.length>0) 
  58.     {
  59.     i.value = i.value.replace(/[^\d]+/g, ''); 
  60.     return true;
  61.     }
  62.     return false;
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.     </script>
  76. </head>
  77.  
  78.  
  79.  
  80.  
  81.  
  82. <body>
  83.     <form id="form1" runat="server">
  84.     <div>
  85.     <table>
  86.     <tr>
  87.  
  88.  
  89.  
  90.     <td style ="border-color:Red ;border-width:3px;width:500px;height :700px; color: #cc0000;" >
  91.  
  92.     <table>
  93.  
  94.     <tr>
  95.     <td  style ="border-color:Red ;border-width:3px;width:479px;height :100px;top :0px; color: #ff3333;">
  96.  
  97.  
  98.     <div id="div1"  runat ="server">
  99.     <table>
  100.     <tr>
  101.     <td  style="width:82px; height: 27px;"></td><td  style="width:82px; height: 27px;">
  102.         <asp:Button ID="All" runat="server" Text="All" OnClick="All_Click" /></td><td style="width:82px; height: 27px;">
  103.         <asp:Button ID="Search" runat="server" Text="Search" OnClick="Search_Click" /></td>
  104.  
  105.     </tr>
  106.  
  107.     </table>
  108.  
  109.     </div>
  110.  
  111.  
  112.     <div id="close" runat ="server" >
  113.     <table>
  114.     <tr><td style ="width:42px" rowspan="2"></td><td style="width:42px; height: 26px;"></td><td style="height: 26px"><asp:Button ID="clo" runat ="server"  Text ="Close" OnClick="clo_Click"/></td></tr>
  115.     <tr><td style="width:42px; height: 26px;"><asp:TextBox ID="txt" runat ="server" ></asp:TextBox>
  116. <%--     <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="setxt"    >
  117.  
  118.           </ajaxToolkit:CalendarExtender>--%></td><td style="height: 26px"><asp:Button ID="setxt" runat ="server"  Text ="Search" OnClick="setxt_Click"/></td></tr>
  119.      <tr><td style ="width:42px"></td><td>
  120.          &nbsp; &nbsp; &nbsp; dd-mm-yyyy</td></tr>
  121.  
  122.  
  123.  
  124.     </table>    
  125.  
  126.  
  127.  
  128.     </div>
  129.  
  130.  
  131.  
  132.  
  133.     </td>
  134.  
  135.     </tr>
  136.     <tr>
  137.     <td  style ="border-color:Red ;border-width:3px;width:479px;height :100px;top :3px;">
  138.         <br />
  139.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false" HeaderStyle-BackColor ="Aqua" >
  140.         <Columns>
  141.         <asp:TemplateField>
  142.  
  143.         <ItemTemplate>
  144.  
  145.         <asp:Label ID="lael1" runat ="server"  Text ='<%#Bind("mastername") %>'></asp:Label>
  146.  
  147.         </ItemTemplate>
  148.  
  149.  
  150.         </asp:TemplateField>
  151.          <asp:TemplateField>
  152.  
  153. <ItemTemplate>
  154.  
  155.        <asp:TextBox ID="textbox1" runat ="server"    onkeyup ="ValidateText(this);"></asp:TextBox>
  156.  
  157.         </ItemTemplate>
  158.  
  159.         <EditItemTemplate>
  160.         <asp:TextBox ID="textbox1" runat ="server"></asp:TextBox>
  161.  
  162.         </EditItemTemplate>
  163.  
  164.  
  165.         </asp:TemplateField>
  166.  
  167.  
  168.         <asp:TemplateField>
  169.  
  170.         <ItemTemplate>
  171.  
  172.        <asp:Button  ID="button1" Text="submit" runat ="server"  OnClick="submit_Click" OnClientClick ="return validation()  " UseSubmitBehavior="false"></asp:Button>
  173.  
  174.         </ItemTemplate>
  175.  
  176.  
  177.         </asp:TemplateField>
  178.  
  179.         </Columns>
  180.         </asp:GridView>
  181.  
  182.     </td></tr>
  183.  
  184.  
  185.     </table>
  186.         &nbsp;&nbsp;
  187.  
  188.  
  189.     </td><td style ="border-color:Red ;border-width:3px;width:200px"></td></tr>
  190.  
  191.  
  192.     </table>
  193.  
  194.     </div>
  195.     </form>
  196. </body>
  197. </html>
Mar 20 '09 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
In your validation() function, you've got duplicate code using the same variable as an index which will cause problems. The code looks very messy with brackets all over the place, but it could be replaced by accessing the element using its ID.
Mar 24 '09 #2

Frinavale
Expert Mod 5K+
P: 9,731
You are going to have to get the ClientID of the TextBox in your VB or C# code.

Once you have that, you're going to have to set the button's onclick attribute to the JavaScript method (which is rather messy, with duplicate code, as acoder has pointed out).

I'm not sure when your button is visible since there are multiple buttons in your code...so... hrmmm

Say your TextBox and Button are made available during RowEditing. In your RowEditing event you would retrieve the ClientID of the TextBox using the FindControl method on the row being edited and then set the onclick attribute of the Button (which you will also have to get using the FindControl method) to call the JavaScript method...passing it the value of your TextBox's ClientID:

Expand|Select|Wrap|Line Numbers
  1. Private Sub myGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles myGridView.RowEditing
  2.         Dim theRowBeingEdited As GridViewRow = myGridView.Rows(e.NewEditIndex)
  3.         Dim myTextBox As TextBox = CType(theRowBeingEdited.FindControl("theTextBox"), TextBox)
  4.         Dim myButton As Button = CType(theRowBeingEdited.FindControl("theButton"), Button)
  5.  
  6.         If myTextBox IsNot Nothing AndAlso myButton IsNot Nothing Then
  7.             myButton.Attributes.Add("onclick", "return JavaScriptFunctionName('" + myTextBox.ClientID + "');")
  8.         End If
  9. End Sub
  10.  
Mar 24 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.