473,789 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't access values via javascript in content page

ThatThatGuy
449 Recognized Expert Contributor
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.ge tElementById("T extBox1").value ;

or
even if it's inside a form

var str=form1.TextB ox1.value;

or
can't even set the value of the textbox....
document.getEle mentById("TextB ox1").value="UG GGHHHHHH";

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 8106
Frinavale
9,735 Recognized Expert Moderator Expert
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 ContentPlaceHol ders 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 ContentPlaceHol ders 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 ContentPlaceHol ders 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_MainConte nt_myTextBox

Or if it was in the footer ContentTemplate Holder it would look like:

ctl00_FooterCon tent_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 Recognized Expert Contributor
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
1121
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 to access the options in a server select control on 'WebForm1.aspx' from 'DisplaySettings.aspx'. Is there an easy way that I can do this? Thanks in advance for any pointers.
4
3016
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. here is the javascript produced: <script>window.opener.document.forms.txtEndDate.value = '7/15/2006';self.close()</script> I basically need to populate txtEndDate on the content page. if
1
310
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I prevent access to a web page by using javascript? ----------------------------------------------------------------------- In practice you can't. While you could create a suitable encryption system with a password in the page, the level of support you need to do this means it's always simpler to do it server-side. Anything that "protects" a page...
6
9161
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" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
1
8488
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 I fix this and display the date into textbox after selecting the date from Calendar.aspx? This Very Urgent .......................... If any body help me out I have given the following Code ******************************
1
1441
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
1921
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 View Source in the browser and see all the controls so I know they are there, yet they vanish once I click OK. This is happening on a Content page. The browser is IE 6.0.2900 and I'm running it inside of Visual Studio 2005. My master page...
3
4434
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 when iam using a normal web page... but can't in content page.... Code in Master Page <%@ Master Language="C#" AutoEventWireup="true" CodeFile="submaster.master.cs" Inherits="submaster" %> <%@ Register Assembly="AjaxControlToolkit"...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.