Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello all,
I have a gridview with textbox template field where I insert Marks of students, now I want to validate the textbox (checking full marks of particular paper),here I need to pass the value of textbox to javascript so that I can validate textbox on onkeypress event of textbox how i can do so..

Or Is there any best alternative..
Posted

Hi Gopal,

Try the below code

var grid = document.getElementById("<%= GridName.ClientID %>");
for (var i=0; i<grid.rows.length; i++){
var row = grid.Rows.getRow(i);
var ActivityTypeCell = row.getCell(10);
var ActivityTypeDropdown = ActivityTypeCell.getElement().childNodes[0];
var value= row.getCell(cellindex).getElement().childNodes[0].value
}

Replace the cellindex with the inxdex of the cell where the text box lies.

Happy Coding
:)
 
Share this answer
 
v2
Comments
Gopal Rakhal 31-May-12 9:40am    
Thanks
Gopal Rakhal 31-May-12 10:34am    
Thanks but I am not understanding you, I want to validate my textboxes inside Gridview. I want to restrict the user that he/she should not enter the marks greater than 80 or else. how can i do so...
here I need to pass the value of textbox to javascript
gridview with textbox template field


Add a client side onkeyup to each textbox in a RowDataBound method where each row is called. Find the textbox using FindControl and then add onclick attribute referring a JS method.

In JS method, get the textbox value and then apply your logic.

Something like:
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{  
  TextBox tb = (TextBox)e.Row.FindControl("TextBox1");
  tb.Attributes.Add("onkeyup", "javascript: ValidateMe(this);"); 
}

JavaScript
function ValidateMe(currtextbox)
{
   // Use currenttextbox.value to validate
}
 
Share this answer
 
Comments
Gopal Rakhal 31-May-12 9:40am    
thanks
Gopal Rakhal 31-May-12 10:30am    
Thanks Sandeep, I tried ur code, but its giving "Object reference not set to an instance of an object." error, Actually i am loading my gridview on pageload, and want to check/validate on onekeypress event..
Sandeep Mewara 31-May-12 14:51pm    
1. I told you somewhat like that - not an exact code (yet i dont see a potential place of null reference if you have a textbox1 defined)
2. Whatever you are doing, rowdata bound will be raised when you bind your grid. Use Visual Studio DEBUGGER and then follow the execution to see how things are working. Use F11.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900