Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I click on button just page refresh and I didn't get textbox value.
C#
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="TestToOnFocus.test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="test.js"></script>
    <title>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="return test();"/>
    </div>
    </form>
</body>
</html>

JavaScript
function test() {

    var name = document.getElementById("<%= txtName.ClientID %>").value;
    if(name=="")
    {
        alert("Please");
    }
    else
    {
        alert(name);
    }
}
Posted
Comments
hypermellow 20-Nov-15 6:46am    
Your JavaScript function needs to return a value of false to prevent the postback.
.. add "return false;" as the last line of your function, and see how it behaves then.
MS Rumi 20-Nov-15 7:06am    
not working
hypermellow 20-Nov-15 7:25am    
Then I would start by checking that your test.js file is included correctly.
... you could also try removing the included js file, and adding the test() function to the document head (within script blocks).

Try this-
JavaScript
function test() {
 
    var name = document.getElementById("<%= txtName.ClientID %>").value;
    if(name=="")
    {
        alert("Please");
        return false;
    }
    else
    {
        alert(name);
        return true;
    }
}
 
Share this answer
 
Comments
MS Rumi 20-Nov-15 7:08am    
no dear its not working .. just page refreshed no text value comes.
HTML
 <script language ="javascript" type ="text/javascript" >

function validate()
{
if(document .getElementById("<%=txtName.ClientID%>").value=="Select")
{

alert ("Please ");
document.getElementById("<%=txtName.ClientID%>").focus();
return false ;

}

}

</script>


http://www.aspsnippets.com/Articles/TextBox-Validation-using-JavaScript.aspx[^]

Download Js file and drag it to ur project
 
Share this answer
 
v4
Comments
MS Rumi 20-Nov-15 7:12am    
just page refreshed
Arasappan 20-Nov-15 7:16am    
Answer Updated
MS Rumi 20-Nov-15 8:20am    
I just want to get the value in the textbox. But I am not getting. :(
Arasappan 20-Nov-15 8:21am    
what you getting
try
document.getElementById("<%= txtName.ClientID %>").val();

instead of
document.getElementById("<%= txtName.ClientID %>").value;
 
Share this answer
 
Comments
MS Rumi 20-Nov-15 8:21am    
Tried both of them but still page refreshed and no value comes out.

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