Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get the value of the input in a javascript, but its not working. Given below is the code of my test page
ASP.NET
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">

    function Validation() {

        var TestVar = document.getElementById('txtName').value;
        alert(TestVar);

    }

</script>
<table>
<tr>
<td align="left"><input type="text"  runat="server" id="txtName" /></td>
</tr>

<tr>
    <td align="left" class="style3">
        <asp:Button runat="server" ID="btnsubmit" Text="Submit" 
        Height="26px" OnClientClick="Validation()"  />
       
        </td></tr>
       
        </table>
</asp:Content>
Posted

First off, why are you putting your table html element into your HeadContent container? Shouldn't you only be putting the script element in the HeadContent while putting the table in the MainContent container?

To fix your issue you need to modify the javascript line so it writes the controls ClientID.

Change this line...
JavaScript
var TestVar = document.getElementById('txtName').value;

To this...
JavaScript
var TestVar = document.getElementById('<%= txtName.ClientID %>').value;

If the control is tagged as runat="Server" then you can't reference the control's id in javascript as it is in the .aspx page because asp.net will pre-pend to the name based on the control's parent controls. For example, your id of 'txtName' id actually becomes something like 'MainContent_txtName' when it gets rendered to the response stream. To address this you do the above where you reference the ClientID in your javascript.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 21-Dec-13 0:51am    
correct
I believe you need to remove the runat="server" from your input. That is not necessary since you want the client to run that function.
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">
 
    function Validation() {
 
        var TestVar = document.getElementById('txtName').value;
        alert(TestVar);
 
    }
 
</script>
<table>
<tr>
<td align="left"><input type="text" id="txtName" /></td>
</tr>
 
<tr>
    <td align="left" class="style3">
        <asp:Button runat="server" ID="btnsubmit" Text="Submit" 
        Height="26px" OnClientClick="Validation()"  />
       
        </td></tr>
       
        </table>
</asp:Content>
 
Share this answer
 
v2
Comments
Member 7909353 20-Dec-13 14:47pm    
But I have to access in c# also i.e. back end
Richard C Bishop 20-Dec-13 14:57pm    
Ok, I just tested that and it works with it in there on my page. What is not working about it?
hi
solution 2 will work fine.. if not try like this,

load the page in browser and right click -> view source , find the txtName control on the page. and note down the id for it..

use that id in your javascript code..
 
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