Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear sir,

Please tell me how to validate textbox control inside the gridview control using onclinetClick() event in javascript.


Ex:
Source code:
XML
<asp:gridview id="grdmenu" runat="server"><FooterTemplate>                                                                                <asp:TextBox ID="txtbox" runat="server"  TextMode="MultiLine">
</asp:TextBox>
   </FooterTemplate>

<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:ImageButton ID="btnMenuUpdate" runat="server" OnClientClick="return val();"
Text="Update"/>
</EditItemTemplate>
</asp:TemplateField
>

javascript Code:

Quote:

function val()
{
debugger;
var strin = document.getElementById(<%= txtbox.ClientID%>").value;
}

but it will raising error? "The name 'txtbox' does not exist in the current context
"
I dont know what is the error, please how to get txtbox value to strin give me code..


Please reply me

By mohan
Posted
Updated 19-Feb-12 22:30pm
v2
Comments
Nilesh Patil Kolhapur 20-Feb-12 4:49am    
are you used master page?

Hey,

Correct Your code there is some syntax error
or replace your code by following
document.getElementById("<%= txtbox.ClientID%>").value;
best luck
 
Share this answer
 
pass parameter to function val()
and call that function in inside gridview with parameter

<asp:textbox id="txtbox" runat="server" textmode="MultiLine" xmlns:asp="#unknown">
 
Share this answer
 
Comments
Member 10290905 7-Jan-14 23:35pm    
how to validate textbox inside gridview in asp.net pls help me
thanks
Hi, not use OnClientClick="return val();"

Use below code:

at rowdatabound event, find control for Button and text box.

then write
C#
button1.attributes.Add("onclick", "javascript:return val('"+txtBx.ClientID+"');");


now, in the JS function, insert one parameter in your function.

JavaScript
function val(txtBx)
{
txtBxObj = document.getElementById(txtBx);
if(txtBxObj.value =="")
{
//alert
return false;
}
}


and check value of textbox.
 
Share this answer
 
C#
var grid=document.getElementById('<%=yourGridID.ClientID%>');
var theTextBoxvalue=grid.rows[1].cells[yourcellIndex].children[0].value;
if(theTextBoxvalue!='')
{
    //go further
}
else
{
    alert('enter text please');
}
 
Share this answer
 

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