473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can't access values via javascript in content page

ThatThatGuy
449 Expert 256MB
There's this aspx page proabably a content page....
There's a provision where i need to access a textbox (ASP or HTML Server)
value can't access it in content page even if i do

var str=document.getElementById("TextBox1").value;

or
even if it's inside a form

var str=form1.TextBox1.value;

or
can't even set the value of the textbox....
document.getElementById("TextBox1").value="UGGGHHH HHH";

And even this doesn't work...

var str=<%=TextBox1.Text%>;

The thing is Content pages don't support <head> tags so i unwillingly had to put all the javascript in the <asp:Content> section...

These above code are otherwise possible in any normal aspx page.....
Can anyone exlplain me the resion for this........
thank you...thanks......
Aug 2 '09 #1
2 8063
Frinavale
9,735 Expert Mod 8TB
When you are using MasterPages your controls are rendered with Unique Client IDs in the browser. These ID's probably will not match the name that you have assigned them in your .NET code.


The reason this happens is because you can load several different pages into the ContentPlaceHolders in the MasterPage. Each of these pages may have the same name for the .NET control.

For example take the following MasterPage:

Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="VB" Inherits="System.Web.Mvc.ViewMasterPage" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
  7.     <link href="Site.css" rel="stylesheet" type="text/css" />
  8. </head>
  9.  
  10. <body>
  11.         <div id="main">
  12.             <asp:ContentPlaceHolder ID="MainContent" runat="server" />
  13.             <asp:ContentPlaceHolder ID="FooterContent" runat="server" />
  14.         </div>
  15.     </div>
  16. </body>
  17. </html>
The above MasterPage has 3 ContentPlaceHolders where you can place content. The first one, the TitleContent, is used to add stuff to the Header section of the page via the content pages using this MasterPage. The second, MainContent, will display the "main content" page, and the last one contains "footer content".

ASP.NET assigns each element within the content pages a unique ID in order to ensure that every element within the ContentPlaceHolders gets a unique ID when it is rendered as HTML in the web browser.

So you may have a content page with a TextBox:
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox id="myTextBox" runat="server" />
But, because this content page is compiled along with the MasterPage the TextBox will end up with an HTML (clientID) of:

ctl00_MainContent_myTextBox

Or if it was in the footer ContentTemplateHolder it would look like:

ctl00_FooterContent_myTextBox


See how it works?

This is the ID that you need to use in your JavaScript.
The way to access this is to use the asp.net control's ClientID property.

So your JavaScript would look something like:

VB
Expand|Select|Wrap|Line Numbers
  1. var str=document.getElementById("<%= myTextBox.ClientID %>").value;
  2.  
C#
Expand|Select|Wrap|Line Numbers
  1. var str=document.getElementById("<%= myTextBox.ClientID(); %>").value;
  2.  

I was recently reading up on JavaScript loading times. Apparently it is best to place your JavaScript at the bottom of the page unless it NEEDS to be at the top (in the head section).

The reason is because of how things are rendered...if it's placed in the top of the page the the HTML and CSS has to wait to render while the JavaScript is being interpreted. But if you place it at the bottom of the page then the HTML and CSS can load quickly and then the JavaScript is interpreted.

So it's not bad to place it at the bottom of the page ...say in a Footer Section ;)
Aug 4 '09 #2
ThatThatGuy
449 Expert 256MB
thanks ....for your explanation....things worked...fine
i knew this...but didn't really clicked
Aug 6 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DL | last post by:
I have a project that has 2 pages, 'WebForm1.aspx' and 'DisplaySettings.aspx'. I have a button on 'WebForm1.aspx' that opens 'DisplaySettings.aspx' in another window. However, I need to be able...
4
by: Mori | last post by:
I am using masterPage and I need to populate a textbox that is in a content control with data from popup page that is not part of the master page. This code works if no masterpage is involved. ...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I prevent access to a web page by using javascript?...
6
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %>...
1
by: sudip2008 | last post by:
When using the Calendar Popup in a content page of a masterpage the strForName is always set to aspnetForm This breaks this line from working properly window.opener.document.forms...... How can...
1
by: pipet2002 | last post by:
how can i send values from one page to another upon clicking a button which links me to that page
0
by: e_spork | last post by:
I am using Page.ClientScript.RegisterStartupScript to throw up a Javascript alert box. When I click OK on the alert box, all the controls on my page disappear and I don't understand why. I can...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.