Click here to Skip to main content
16,011,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in external javascript file calling a asp textbox control is not working in master page.
for eg this is my code in external js file

JavaScript
function name_valid()
 {
  var a=document.getElementById("name_box");
  if(a==null || a=="")
  {
   alert('Enter the name');
  }

  var b=document.getElementById("dob_box");
  if( b==null || b=="")
  {
   alert('Enter the Date Of Birth');

  }
 }


content page: content place holder(head):

ASP.NET
<script  src="valid.js" type="text/javascript" language="javascript"  ></script>
contentplaceholder1:

<asp:TextBox ID="name_box" runat="server" >

<asp:Textbox Id="dob_box" runat="server">

<asp:Button ID="submit_button" runat="server" Text="Submit" OnClientClick="name_valid()"/>


here javascript executes the the statement "alert('Enter the name')" even the "name_box" textbox is empty or some name is entered in it kindly help me to solve this problem
Posted
Updated 10-Jan-12 2:08am
v2

Keep in mind the ASP.NET will mangle the control's ID, especially when in containers such as content placeholders used for Masterpages. Look at the actual rendered output and you will see this control is actually something like ctrl001_name_box.

You would be better off, IMO, using Jquery and doing something like this
JavaScript
var name_box = $("[id$='name_box']");

This will find the element whose id attribute ends with name_box, regardless of how many prefixes ASP.NET adds.
 
Share this answer
 
Comments
sarathysarathy 10-Jan-12 8:24am    
Kindly help me in solving this using external javascript.
fjdiewornncalwe 10-Jan-12 9:59am    
Mark has told you how to do it. It is up to you to try, not for us to do it for you by providing the code. This is a very basic asp.net issue and you will learn the most by trying yourself based on the information given.
try to add the attribute ClientIDMode="Static" to your textbox's it may work fine.
 
Share this answer
 
Comments
[no name] 10-Jan-12 10:59am    
This property is only available in 4.0 and above.

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