A.Venkatesan
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
Hi, the “onclick” attribute of Button is a server attribute and it will trigger C# codes (or VB codes) rather than JavaScript codes. If you want to get the Textbox value via Javascript, please try “onClientClick” attribute, for example:
scriptkid
Member
30 Points
21 Posts
Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 09:48 AM|LINK
Hi people,
I would like to pass the value of the textbox entered by the user to the javascript funtion when the user clicks the SUBMIT button.
I used this following code:
<script type="text/javascript"> function checkit(test2) { <!--something--> } </script> <asp:TextBox ID="rowID" runat="server" ontextchanged="rowID_TextChanged"> </asp:TextBox> <asp:Button ID="SearchBtn" runat="server" Text="Search" onclick="checkit(this.form.rowID.value)" />karthicks
All-Star
31378 Points
5422 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 10:05 AM|LINK
hi, try below code <script> function checkit() { var value = document.getElementById('<%=rowID.ClientID %>').value; alert(value); } </script> <asp:TextBox ID="rowID" runat="server"></asp:TextBox> <asp:Button ID="SearchBtn" runat="server" Text="Search" OnClientClick="checkit()" />to call javascript function in button , you should use OnClientClick,
so refer below code
<script> function checkit(test2) { alert(test2); <!--something--> } </script> <asp:TextBox ID="rowID" runat="server"></asp:TextBox> <asp:Button ID="SearchBtn" runat="server" Text="Search" OnClientClick="checkit(this.form.rowID.value)" />Karthick S
scriptkid
Member
30 Points
21 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 10:10 AM|LINK
its not working.. the values were not passed to the javascript. function :(
vinay13mar
Star
7756 Points
1626 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 10:16 AM|LINK
Check the code
<script type="text/javascript"> function checkit() { <!--something--> alert(document.getElementByID(<%=rowID.ClientID %>).value); } </script> <asp:TextBox ID="rowID" runat="server" ontextchanged="rowID_TextChanged"> </asp:TextBox> <asp:Button ID="SearchBtn" runat="server" Text="Search" OnClientClick="javascript:checkit(this)" />V.K.Singh
venkatmca008
Participant
1810 Points
341 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 10:24 AM|LINK
hi..try this..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datetime.aspx.cs" Inherits="datetime" %> <html> <head> <script> function CheckPromotionalCode(obj) { var jva= document.getElementById(obj).value; alert(jva); }</script> </head> <body> <form id="Form1" runat="server"> Name: <asp:TextBox ID="txtName" runat="server" /> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="CheckPromotionalCode('txtName');"> </asp:Button><br /> </form> </body> </html>Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
scriptkid
Member
30 Points
21 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 12:41 PM|LINK
i am sorry.. none of them are working :(
vijay_myl
Contributor
5070 Points
1068 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 02, 2012 01:20 PM|LINK
hi..try thiscode..
<div> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </div> <div> <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick ="txt();"/> </div><script type ="text/javascript" > function txt() { var t = document.getElementById("TextBox2"); alert(t.value); } </script>My .NET blog
Submit Article
Allen Li - M...
Star
10411 Points
1196 Posts
Re: Passing Textbox value to javascript function when user hits Submit Button in asp.net
Mar 05, 2012 01:47 AM|LINK
Hi, the “onclick” attribute of Button is a server attribute and it will trigger C# codes (or VB codes) rather than JavaScript codes. If you want to get the Textbox value via Javascript, please try “onClientClick” attribute, for example:
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework