473,657 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validation of textbox in gridview

8 New Member
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
2 12887
acoder
16,027 Recognized Expert Moderator MVP
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
9,735 Recognized Expert Moderator Expert
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...passin g 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

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

Similar topics

2
2521
by: Walter | last post by:
I have a GridView using Template columns. In my EditItemTemplate I am using CompareValidators to check data types. When validation fails, I would like to change the border of the textbox in question. I thought I could loop through the ValidatorCollection during the RowUpdating event but couldn't get the reference to the textbox. When I made an explicit reference to a textbox I got the message that this would need to be done in the...
1
1813
by: Buddy Ackerman | last post by:
I don't know what the problem is. I have a form with several controls that need to be validated, I put a validation group in every form control, every validatoino control, the submit button and the validation summary control. I want it to do client side validation so I set every validation control display property to none and set the validation summary control to ShowMessage to true and ShowSummary to false. Als form controls set...
1
256
by: KenLee | last post by:
I use asp 2.0 net. what is the same for "onblur" in asp control such as "<asp:textbox / >"? I use "<asp:textbox />" inside Gridview. I try to validation using client side. when I try using "<input type="text" />" then validation is not hard thing. however using asp.net is hard.
12
2041
by: Dabbler | last post by:
I need to insure that at least one of three phone number fields has a value (requiredfield) but I'm not sure of a way to implement this without server side logic. Is there a way to use the validation controls to do this? Thanks.
2
5852
by: JaM | last post by:
Hi all, I have created a gridview vith dynamic textbox columns (they are in variable number, it depends on what things I select from database) aspx code: __________________________________________ <asp:GridView AutoGenerateColumns="false" ID="GridView2" Runat="server" <Columns>
1
5155
by: naasirsom | last post by:
Dear sir, I am using Gridview asp.net 2.0, I have a problem with Gridview Footer Template. I have put Button, Textbox and RequiredValidator in the Footer Template and Want to validate the textbox if it is empty and I used ValidationGroup . I have done all jobs and the page is working but the problem is the button is working the first time but the second time does not postback.
1
1267
by: Bhagyasree | last post by:
Hi, I want to validate the textbox value in the gridview while updating gridview.Iam using Link buttons to edit,update and delete.Iam not getting how to get the updated value of textbox using javascript.Iam adding attributes to the link button update.plz anybody give solution to my problem
12
9893
by: btreddy | last post by:
Hii Experts, im facing a problem with the javascript validation.i've a gridview in my web form and added a footer tempalte and placed textboxes in tht tempalte ,just to add a new row to the gridview and i wanna to validate the emalid entered in the footertextbox of the gridview. so i added a javasript function : function OnCheckEmailID(srcID) { var email=document.getElementById(srcID); var filter...
0
1108
parshupooja
by: parshupooja | last post by:
hey all, I have DataTable binded to Gridview . This Datatable contains Data from Database. Before binding it to Gridview I am adding extra line with few controls dynamically in it such as text box below. When page loads I see extra Text box on the bottom of gridview but Validation is not working. Any Idea? I am calling this Function on each page load. Private Sub AddControls() Dim con As SqlConnection = New...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.