Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

getElementById doesn't seem to work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 06 Sep 2007 00:08 GMT
I have an aspx window that is calling another window and I am trying to get
a couple of objects from the opening page.  The problem is that I can't seem
to get anything inside of my form.

I have the following in my opener page:

<body id="myBody" leftmargin="0" topmargin="0" marginwidth="0"
marginheight="0" runat="server">
 <form id="addForm" runat="server">
    <asp:label id="theBox" text = "a test" runat="server"/>
    <asp:PlaceHolder ID="thePlaceHolder" runat="server"/>
 </form>
</body>

The "thePlaceHolder" object is where I put my User object.

I am trying to get to some of the objects inside of this user object and if
I walk the Dom I can see some (but not all the objects).

I can't even seem to get to the "theBox" object which is one of the first
objects in the Dom.  I tried to get the object like this:

function entry()
{
alert("On Entry");
var theObject = window.opener.document.getElementById('theBox');
alert("After Entry theObject = " + theObject);

alert("length = " + window.opener.document.addForm.length);

for (var i = 0;i< window.opener.document.addForm.length;i++)
{
    alert("inside for loop i = " + i);
    var e =  window.opener.document.addForm.elements[i];
    alert("e.type = " + e.type);
    alert("e.name = " + e.name);
}
}

theObject ends up being null.  If I move the "theBox" object outside of the
form, it seems to find it fine.

When I walk the Dom, the length is about 25 and I know there are about 200
objects on the page.  Some of the objects are being shown inside of the User
control - but "theBox" doesn't show at all.  And it is one of the first
ones.

The trace shows the first part of the objects as:

__PAGE ASP.ResumeSubmittalForm_aspx
 _ctl1 System.Web.UI.LiteralControl
   _ctl2 System.Web.UI.LiteralControl
   MyStyleSheet System.Web.UI.HtmlControls.HtmlGenericControl
   _ctl3 System.Web.UI.LiteralControl
   myBody System.Web.UI.HtmlControls.HtmlGenericControl
       _ctl4 System.Web.UI.LiteralControl
       addForm System.Web.UI.HtmlControls.HtmlForm
           _ctl5 System.Web.UI.LiteralControl
           theBox System.Web.UI.WebControls.Label
           _ctl6 System.Web.UI.LiteralControl
           thePlaceHolder System.Web.UI.WebControls.PlaceHolder
               _ctl0 ASP.mainPage_ascx
                   _ctl0:_ctl2 System.Web.UI.ResourceBasedLiteralControl
                   _ctl0:HomeLink System.Web.UI.WebControls.HyperLink
                   _ctl0:_ctl3 System.Web.UI.LiteralControl
                   _ctl0:MyInformationLink
System.Web.UI.WebControls.HyperLink
                   _ctl0:_ctl4 System.Web.UI.LiteralControl
                   _ctl0:ResumeLink System.Web.UI.WebControls.HyperLink
                   _ctl0:_ctl5 System.Web.UI.LiteralControl
                   _ctl0:CoverLetterLink
System.Web.UI.WebControls.HyperLink
                   _ctl0:_ctl6 System.Web.UI.LiteralControl
                   _ctl0:PassportLink System.Web.UI.WebControls.HyperLink

The Forms collection shows as:

__EVENTTARGET _ctl0:_ctl1:PreviewLink
__EVENTARGUMENT
__VIEWSTATE dDwzNj
_ctl0:_ctl1:resumes 0
ctl0__ctl1_ResumeText
ctl0__ctl1_CoverLetter
_ctl0:_ctl1:GenderList
_ctl0:_ctl1:EthnicityList
_ctl0:_ctl1:VeteranStatus
_sk_scrollkeepervalue 0!0

Where is the "theBox" control and when walking the Dom it show the length as
25.

What is going on?????

Is this a problem with asp.net 1.1????

Thanks,

Tom
tshad - 06 Sep 2007 00:15 GMT
In FireFox, the length show 3 so doesn't work at all.

Not sure how to get this to work.

Thanks,

Tom

>I have an aspx window that is calling another window and I am trying to get
>a couple of objects from the opening page.  The problem is that I can't
[quoted text clipped - 94 lines]
>
> Tom
bruce barker - 06 Sep 2007 01:14 GMT
as the post below said, you need the id rendered (control.ClientId).

also the form.elements collection only contains form controls
(input,select and textarea). only form elements have a type and name
property.

also as firefox is strict, you need to specify:

    document.addForm.elements

to find dom objects like a span or anchor (link), you want to walk
childNodes recursively.

-- bruce (sqlwork.com)

> In FireFox, the length show 3 so doesn't work at all.
>
[quoted text clipped - 102 lines]
>>
>> Tom
tshad - 06 Sep 2007 02:22 GMT
> as the post below said, you need the id rendered (control.ClientId).
>
[quoted text clipped - 8 lines]
> to find dom objects like a span or anchor (link), you want to walk
> childNodes recursively.

You're right on the span.  I hadn't realized I was using a Label (when I
changed it to a textbox it worked fine).  But I do need to get the
Label/Span and my walking the nodes is not working correctly.

I am doing the following:

alert("length = " + document.addForm.length);
for (var i = 0;i< document.addForm.length;i++)
{
 alert("inside for loop i = " + i);
 var e =  document.addForm.elements[i];
 alert("e.type = " + e.type);
 alert("e.name = " + e.name);
}

But this doesn't seem to find the span tag (which has an id).  I assume I am
not following the whole path.

How would I change this so I would find the span tag?

Thanks,

Tom

> -- bruce (sqlwork.com)
>
[quoted text clipped - 105 lines]
>>>
>>> Tom
bruce barker - 06 Sep 2007 03:54 GMT
as i said, you need to use the childNodes, and a span does not have a
type or name property.

  var nodes = document.forms[0].childNodes;
  for (var i=0; i < nodes.length; ++i)
  {
    alert("tag: " + node[i].tagName + " id: " + nodes.id);
  }

or lookup the span by id.

  var theBox = document.getElementById('<%=theBox.ClientId%>');

-- bruce (sqlwork.com)

>> as the post below said, you need the id rendered (control.ClientId).
>>
[quoted text clipped - 142 lines]
>>>>
>>>> Tom
tshad - 06 Sep 2007 18:08 GMT
> as i said, you need to use the childNodes, and a span does not have a type
> or name property.
[quoted text clipped - 8 lines]
>
>   var theBox = document.getElementById('<%=theBox.ClientId%>');

I tried that but it gives me an error:

*******************************************************
Compiler Error Message: BC30451: Name 'ctl0_ResumeDisplay_objResumeText' is
not declared.

Source Error:

Line 47: // var toResumeText =
document.getElementById('ctl0_ResumeDisplay_objResumeText');
Line 48:  var toResumeText =
document.getElementById('<%=ctl0_ResumeDisplay_objResumeText.ClientID%>');
Line 50:  var toCoverLetter =
document.getElementById('ctl0_ResumeDisplay_objCoverLetterTextBox');
*******************************************************

Here is the code:

var fromResumeText =
window.opener.document.getElementById('ctl0__ctl1_ResumeText');
var fromCoverLetter =
window.opener.document.getElementById('ctl0__ctl1_CoverLetter');
// var toResumeText =
document.getElementById('ctl0_ResumeDisplay_objResumeText');
var toResumeText =
document.getElementById('<%=ctl0_ResumeDisplay_objResumeText.ClientID%>');
var toCoverLetter =
document.getElementById('ctl0_ResumeDisplay_objCoverLetterTextBox');

I am looking for 2 objects on my opener page and 2 from my current page.  I
can find all the objects except the 'ctl0_ResumeDisplay_objResumeText' which
is the only object that is an asp:Label.

This code worked fine except that it didn't find Label objects - it found
the other 3.

This error happened when I added the <%=...%> format.

The interesting thing is that I get the error even when I comment out the
new line and the error is on the commented out line????

***************************************************************************
Compiler Error Message: BC30451: Name 'ctl0_ResumeDisplay_objResumeText' is
not declared.

Source Error:

Line 47:  var toResumeText =
document.getElementById('ctl0_ResumeDisplay_objResumeText');
Line 48: // var toResumeText =
document.getElementById('<%=ctl0_ResumeDisplay_objResumeText.ClientID%>');
Line 50:  var toCoverLetter =
document.getElementById('ctl0_ResumeDisplay_objCoverLetterTextBox');
******************************************************************************************The error is on line 48.How can that be???  It is commented out.The trace for this page is:_ctl0:ResumeDisplay:objResumeText System.Web.UI.WebControls.Label_ctl0:ResumeDisplay:_ctl51 System.Web.UI.LiteralControl_ctl0:ResumeDisplay:objCoverLetterTextBox FreeTextBoxControls.FreeTextBox_ctl0:ResumeDisplay:_ctl30 System.Web.UI.LiteralControlIt says that 'ctl0_ResumeDisplay_objResumeText'  doesn't exist.  I don't getthe error message if I don't use the <%=...%> format.  It doesn't find it,but I don't get an error message just a null.Thanks,Tom>> -- bruce (sqlwork.com)>> tshad wrote:>> "bruce barker" <nospam@nospam.com> wrote in messagenews:%23Yg51rB8HHA.5752@TK2MSFTNGP04.phx.gbl...>>> as the post below said, you need the id rendered (control.ClientId).>>>>>> also the form.elements collection only contains form controls(input,select and textarea). only form elements have a type and nameproperty.>>>>>> also as firefox is strict, you need to specify:>>>>>> document.addForm.elements>>>>>> to find dom objects like a span or anchor (link), you want to walkchildNodes recursively.>>>>>>> You're right on the span.  I hadn't realized I was using a Label (when Ichanged it to a textbox it worked fine).  But I do need to get theLabel/Span and my walking the nodes is not working correctly.>>>> I am doing the following:>>>>  alert("length = " + document.addForm.length);>>  for (var i = 0;i< document.addForm.length;i++)>>  {>>   alert("inside for loop i = " + i);>>   var e =  document.addForm.elements[i];>>   alert("e.type = " + e.type);>>   alert("e.name = " + e.name);>>  }>>>> But this doesn't seem to find the span tag (which has an id).  I assume Iam not following the whole path.>>>> How would I change this so I would find the span tag?>>>> Thanks,>>>> Tom>>>>> -- bruce (sqlwork.com)>>>>>> tshad wrote:>>>> In FireFox, the length show 3 so doesn't work at all.>>>>>>>> Not sure how to get this to work.>>>>>>>> Thanks,>>>>>>>> Tom>>>>>>>> "tshad" <t@home.com> wrote in messagenews:eRlUdGB8HHA.3624@TK2MSFTNGP05.phx.gbl...>>>>> I have an aspx window that is calling another window and I am tryingto get a couple of objects from the opening page.  The problem is that Ican't seem to get anything inside of my form.>>>>>>>>>> I have the following in my opener page:>>>>>>>>>> <body id="myBody" leftmargin="0" topmargin="0" marginwidth="0"marginheight="0" runat="server">>>>>>  <form id="addForm" runat="server">>>>>>     <asp:label id="theBox" text = "a test" runat="server"/>>>>>>     <asp:PlaceHolder ID="thePlaceHolder" runat="server"/>>>>>>  </form>>>>>> </body>>>>>>>>>>> The "thePlaceHolder" object is where I put my User object.>>>>>>>>>> I am trying to get to some of the objects inside of this user objectand if I walk the Dom I can see some (but not all the objects).>>>>>>>>>> I can't even seem to get to the "theBox" object which is one of thefirst objects in the Dom.  I tried to get the object like this:>>>>>>>>>> function entry()>>>>> {>>>>> alert("On Entry");>>>>> var theObject = window.opener.document.getElementById('theBox');>>>>> alert("After Entry theObject = " + theObject);>>>>>>>>>> alert("length = " + window.opener.document.addForm.length);>>>>>>>>>> for (var i = 0;i< window.opener.document.addForm.length;i++)>>>>> {>>>>>     alert("inside for loop i = " + i);>>>>>     var e =  window.opener.document.addForm.elements[i];>>>>>     alert("e.type = " + e.type);>>>>>     alert("e.name = " + e.name);>>>>> }>>>>> }>>>>>>>>>> theObject ends up being null.  If I move the "theBox" object outsideof the form, it seems to find it fine.>>>>>>>>>> When I walk the Dom, the length is about 25 and I know there are about200 objects on the page.  Some of the objects are being shown inside of theUser control - but "theBox" doesn't show at all.  And it is one of the firstones.>>>>>>>>>> The trace shows the first part of the objects as:>>>>>>>>>> __PAGE ASP.ResumeSubmittalForm_aspx>>>>>  _ctl1 System.Web.UI.LiteralControl>>>>>    _ctl2 System.Web.UI.LiteralControl>>>>>    MyStyleSheet System.Web.UI.HtmlControls.HtmlGenericControl>>>>>    _ctl3 System.Web.UI.LiteralControl>>>>>    myBody System.Web.UI.HtmlControls.HtmlGenericControl>>>>>        _ctl4 System.Web.UI.LiteralControl>>>>>        addForm System.Web.UI.HtmlControls.HtmlForm>>>>>            _ctl5 System.Web.UI.LiteralControl>>>>>            theBox System.Web.UI.WebControls.Label>>>>>            _ctl6 System.Web.UI.LiteralControl>>>>>            thePlaceHolder System.Web.UI.WebControls.PlaceHolder>>>>>                _ctl0 ASP.mainPage_ascx>>>>>                    _ctl0:_ctl2System.Web.UI.ResourceBasedLiteralControl>>>>>                    _ctl0:HomeLink System.Web.UI.WebControls.HyperLink>>>>>                    _ctl0:_ctl3 System.Web.UI.LiteralControl>>>>>                    _ctl0:MyInformationLinkSystem.Web.UI.WebControls.HyperLink>>>>>                    _ctl0:_ctl4 System.Web.UI.LiteralControl>>>>>                    _ctl0:ResumeLinkSystem.Web.UI.WebControls.HyperLink>>>>>                    _ctl0:_ctl5 System.Web.UI.LiteralControl>>>>>                    _ctl0:CoverLetterLinkSystem.Web.UI.WebControls.HyperLink>>>>>                    _ctl0:_ctl6 System.Web.UI.LiteralControl>>>>>                    _ctl0:PassportLinkSystem.Web.UI.WebControls.HyperLink>>>>>>>>>> The Forms collection shows as:>>>>>>>>>> __EVENTTARGET _ctl0:_ctl1:PreviewLink>>>>> __EVENTARGUMENT>>>>> __VIEWSTATE dDwzNj>>>>> _ctl0:_ctl1:resumes 0>>>>> ctl0__ctl1_ResumeText>>>>> ctl0__ctl1_CoverLetter>>>>> _ctl0:_ctl1:GenderList>>>>> _ctl0:_ctl1:EthnicityList>>>>> _ctl0:_ctl1:VeteranStatus>>>>> _sk_scrollkeepervalue 0!0>>>>>>>>>> Where is the "theBox" control and when walking the Dom it show thelength as 25.>>>>>>>>>> What is going on?????>>>>>>>>>> Is this a problem with asp.net 1.1????>>>>>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
tshad - 06 Sep 2007 18:37 GMT
>> as i said, you need to use the childNodes, and a span does not have a
>> type or name property.
[quoted text clipped - 4 lines]
>> alert("tag: " + node[i].tagName + " id: " + nodes.id);
>>   }

I tried the following but it quit in the for loop.  I added a couple of
alerts to see what caused it to fail:
****************************************
  var nodes = document.forms[0].childNodes;

 alert("nodes = " + nodes + "   nodes.length = " + nodes.length);

  for (var i=0; i < nodes.length; ++i)
  {
      alert("Inside for loop - i = " + i);
      alert("tag: = " + node[i].tagName);
      alert("id: = " + nodes.id);
      alert("tag: " + node[i].tagName + " id: " + nodes.id);
  }

alert("length = " + document.addForm.length);
*****************************************

I get "nodes = [object]" and "nodes.length = 24".  I also get one "inside
for loop - i = 0" message but then I don't get anymore messages.  So the
node[i].tagName gives me an error.

Thanks,

Tom

>> or lookup the span by id.
>>
[quoted text clipped - 141 lines]
> 25.>>>>>>>>>> What is going on?????>>>>>>>>>> Is this a problem with
> asp.net 1.1????>>>>>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
Lit - 06 Sep 2007 18:57 GMT
tshad,

This is wrong.
document.getElementById('<%=ctl0_ResumeDisplay_objResumeText.ClientID%>');

it looks like you are getting the ClientId on the clientId

<%=The_ID_Of_Your_Server_Control.ClientId%>

What is the ID of your Label

if the ServerSide ID of your Label is  MyLabel123  then use
<%=MyLabel123.ClientID%>  -- verify syntax

Make sure Label is runat="server"

Lit

>> as i said, you need to use the childNodes, and a span does not have a
>> type or name property.
[quoted text clipped - 150 lines]
> 25.>>>>>>>>>> What is going on?????>>>>>>>>>> Is this a problem with
> asp.net 1.1????>>>>>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
tshad - 06 Sep 2007 19:12 GMT
> tshad,
>
[quoted text clipped - 6 lines]
>
> What is the ID of your Label

The asp.net line is:

<asp:Label ID="objResumeText" runat="server"/>

in the View Source it is:

<span id="_ctl0_ResumeDisplay_objResumeText"></span>

Could the error be because the objResumeText is in a User control
(ResumeDisplay)?

Thanks,

Tom

> if the ServerSide ID of your Label is  MyLabel123  then use
> <%=MyLabel123.ClientID%>  -- verify syntax
[quoted text clipped - 158 lines]
>> 25.>>>>>>>>>> What is going on?????>>>>>>>>>> Is this a problem with
>> asp.net 1.1????>>>>>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
tshad - 06 Sep 2007 20:11 GMT
>> tshad,
>>
[quoted text clipped - 17 lines]
> Could the error be because the objResumeText is in a User control
> (ResumeDisplay)?

I assume that the problem is the User Controls, because I created a Label on
my main page with id of "theLabel" and this worked fine:

var toResumeText = document.getElementById('<%=theLabel.ClientID%>');

Maybe the problem is related to the fact that you cannot even comment
problem line without getting an error which makes no sense to me.

I tried:

// var toResumeText =
document.forms[0].getElementById('<%=_ctl0_ResumeDisplay_objResumeText.ClientID%>');

and

/* var toResumeText =
document.forms[0].getElementById('<%=_ctl0_ResumeDisplay_objResumeText.ClientID%>');
*/

Both still give me the error that _ctl0_ResumeDisplay_objResumeText doesn't
exist - which it does.  But even if it didn't, the commenting out should
prevent the error.  This is the first time I have not been able comment a
line out.

Thanks,

Tom

> Thanks,
>
[quoted text clipped - 164 lines]
>>> on?????>>>>>>>>>> Is this a problem with asp.net 1.1????>>>>>>>>>>
>>> Thanks,>>>>>>>>>> Tom>>>>>>>
Lit - 06 Sep 2007 20:30 GMT
use this

var toResumeText = document.getElementById('<%=objResumeText.ClientID%>');

DO NOT USE THIS

        _ctl0_ResumeDisplay_objResumeText.ClientID

       _ctl0_ResumeDisplay_objResumeText   is the ClientID

Lit

>>> tshad,
>>>
[quoted text clipped - 218 lines]
>>>> on?????>>>>>>>>>> Is this a problem with asp.net 1.1????>>>>>>>>>>
>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
tshad - 06 Sep 2007 22:30 GMT
> use this
>
> var toResumeText = document.getElementById('<%=objResumeText.ClientID%>');

Didn't work in the .aspx page.

Just copied and pasted it in but I get the same error (except it now says
that objResumeText doesn't exist).

Here is the object in the .ascx page:

       <tr bgcolor="#F3F3F3">
         <td><p style="margin-left:10px"><asp:Label ID="objResumeText"
runat="server"/>
          <br> <br>
          </td>
       </tr>

Here is the object in the View Source (after I take the line out so the page
loads correctly).

       <tr bgcolor="#F3F3F3">
         <td><p style="margin-left:10px"><span
id="_ctl0_ResumeDisplay_objResumeText"></span>
          <br> <br>
             </td>
       </tr>

I am getting this error before the function is called as I have at least 3
alert statements before the getElementById statement.

It probably has something to do with the Javascript being in the .aspx page
and objResumeText being in the .ascx page that is being called by the .aspx
page.

But since this is being called onload in the <body> tag, I can't put the
Javascript in the .ascx file (I don't think).

The body tag is:

<body id="myBody" leftmargin="0" topmargin="0" marginwidth="0"
marginheight="0" runat="server">

and the onload is being set in the Page_Load function:

Sub Page_Load(sender as Object, e as EventArgs)
 Dim thePage as String
   myBody.Attributes.Add("onLoad","entry()")
 Dim pageControl as Control

I finally got it to work by moving the script section over to the .ascx
(User Control) page.  I wasn't sure it would work as I thought it may cough
up on the myBody.Attributes.Add("onLoad","entry()") statement in the .aspx
file since now the entry() function is in the .ascx file.  I thought it may
say that entry() doesn't exist.

Apparently, that wasn't the case as it works now.

I think it is interesting that

var toResumeText =
document.getElementById('ctl0_ResumeDisplay_objResumeText');

works in the .aspx file and

var toResumeText = document.getElementById('<%=objResumeText.ClientID%>');

does not.

I am still confused why comments don't work in this case.

Thanks,

Tom

> DO NOT USE THIS
>
[quoted text clipped - 226 lines]
>>>>> on?????>>>>>>>>>> Is this a problem with asp.net 1.1????>>>>>>>>>>
>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
Lit - 06 Sep 2007 23:00 GMT
I would delete the label and recreate it.
Something is strange.  I don't know why it does not work.

Lit

>> use this
>>
[quoted text clipped - 302 lines]
>>>>>> on?????>>>>>>>>>> Is this a problem with asp.net 1.1????>>>>>>>>>>
>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
tshad - 06 Sep 2007 23:35 GMT
>I would delete the label and recreate it.
> Something is strange.  I don't know why it does not work.

You're right.

I deleted and recreated the Label and it worked.  I think I may have been
missing the "__" at the beginning of the ID.

I did find that the following getElementByID format is the one you have to
use if the control is on a different page.  This means you would need to
look at the View Source to find out the actual ID.  This is what
<%=objResumeText.ClientID%> does for you.

document.getElementById('_ctl0_ResumeDisplay_objResumeText');

The following getElementByID is better since you don't need to know what the
ID will render to.  But the control MUST be on the same page or User
Control.  If you are using multiple controls, this wouldn't work.  You will
get an "objResumeText" does not exist - even though it will on the full page
after rendering.

document.getElementById('<%=objResumeText.ClientID%>');

Thanks for all the help,

Tom

> Lit
>
[quoted text clipped - 306 lines]
>>>>>>> going on?????>>>>>>>>>> Is this a problem with asp.net
>>>>>>> 1.1????>>>>>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
Mark Rae [MVP] - 06 Sep 2007 20:57 GMT
> Maybe the problem is related to the fact that you cannot even comment
> problem line without getting an error which makes no sense to me.
[quoted text clipped - 12 lines]
> Both still give me the error that _ctl0_ResumeDisplay_objResumeText
> doesn't exist - which it does.

Not sure about why that is exactly but, to answer your question about
commenting, you need to comment both the client-side *AND* server side
script e.g.

// var toResumeText =
document.forms[0].getElementById('<%//=_ctl0_ResumeDisplay_objResumeText.ClientID%>');

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

tshad - 06 Sep 2007 22:33 GMT
>> Maybe the problem is related to the fact that you cannot even comment
>> problem line without getting an error which makes no sense to me.
[quoted text clipped - 19 lines]
> // var toResumeText =
> document.forms[0].getElementById('<%//=_ctl0_ResumeDisplay_objResumeText.ClientID%>');

Really?

Why is that?  Comments are supposed to be ignored.  Lines that are commented
by /* */  could span 20 lines.  But if this line is in the commented lines
it will get the error.

What other things cause this problem with comments?

Thanks,

Tom
Mark Rae [MVP] - 07 Sep 2007 00:34 GMT
>> Not sure about why that is exactly but, to answer your question about
>> commenting, you need to comment both the client-side *AND* server side
[quoted text clipped - 7 lines]
> Why is that?  Comments are supposed to be ignored.  Lines that are
> commented by /* */  could span 20 lines.

<%=...%> basically say "inject a string literal into the HTML markup" - it
has no concept of whether the string that it's injecting resides within a
commented block of JavaScript or not...

> But if this line is in the commented lines it will get the error.

That's right - so comment it out and you won't...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

tshad - 07 Sep 2007 01:26 GMT
>>> Not sure about why that is exactly but, to answer your question about
>>> commenting, you need to comment both the client-side *AND* server side
[quoted text clipped - 11 lines]
> has no concept of whether the string that it's injecting resides within a
> commented block of JavaScript or not...

Is this the only construct that causes this problem?

Thanks,

Tom

>> But if this line is in the commented lines it will get the error.
>
> That's right - so comment it out and you won't...
Mark Rae [MVP] - 07 Sep 2007 09:34 GMT
>>> Why is that?  Comments are supposed to be ignored.  Lines that are
>>> commented by /* */  could span 20 lines.
[quoted text clipped - 4 lines]
>
> Is this the only construct that causes this problem?

I'm not sure what "problem" you're talking about - this is totally expected
behaviour...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

tshad - 07 Sep 2007 17:06 GMT
>>>> Why is that?  Comments are supposed to be ignored.  Lines that are
>>>> commented by /* */  could span 20 lines.
[quoted text clipped - 7 lines]
> I'm not sure what "problem" you're talking about - this is totally
> expected behaviour...

It may be expected behavior to you, but I had never heard of it.  I have
been programming for years and a comment is a comment.  Anything inside a
comment tag, regardless of language, is ignored.

Apparently that is not the case with the <%=...%> construct.

What you are saying is that comments are only partially comments.  If they
have certain characters in them, then they aren't???

You can normally, in other languages (of course HTML is not really a
language), comment whole sections of code for explanations, debugging or
just to be able to use later.

Tom
tshad - 06 Sep 2007 19:07 GMT
I changed the ID to be what was in the View Source to see if that was the
problem, but got the same error:

Compiler Error Message: BC30451: Name '_ctl0_ResumeDisplay_objResumeText' is
not declared.

Source Error:

Line 46:  alert("After Entry fromCoverLetter = " + fromCoverLetter);
Line 47: // var toResumeText =
document.getElementById('ctl0_ResumeDisplay_objResumeText');
Line 48:  var toResumeText =
document.forms[0].getElementById('<%=_ctl0_ResumeDisplay_objResumeText.ClientID%>');
Line 49:  alert("After Entry toResumeText = " + toResumeText);
Line 50:  var toCoverLetter =
document.getElementById('ctl0_ResumeDisplay_objCoverLetterTextBox');

The error is on line 48.

The lines in the ViewSource is:

<tr bgcolor="#F3F3F3">
<td><p style="margin-left:10px"><span
id="_ctl0_ResumeDisplay_objResumeText"></span>
 <br> <br>
</td>
</tr>

As you can see, the id =_ctl0_ResumeDisplay_objResumeText but the comiler
says it doesn't exist.

Thanks,

Tom

>> as i said, you need to use the childNodes, and a span does not have a
>> type or name property.
[quoted text clipped - 150 lines]
> 25.>>>>>>>>>> What is going on?????>>>>>>>>>> Is this a problem with
> asp.net 1.1????>>>>>>>>>> Thanks,>>>>>>>>>> Tom>>>>>>>
Lit - 06 Sep 2007 00:47 GMT
View the source and find out the exact id of the element.
use the server-side element-id .clientID

<%=ServerSideControlID.ClientID%>

Lit

>I have an aspx window that is calling another window and I am trying to get
>a couple of objects from the opening page.  The problem is that I can't
[quoted text clipped - 94 lines]
>
> Tom
tshad - 06 Sep 2007 02:23 GMT
> View the source and find out the exact id of the element.
> use the server-side element-id .clientID
>
> <%=ServerSideControlID.ClientID%>

Where would I put this and how would I use it?

Thanks,

Tom

> Lit
>
[quoted text clipped - 96 lines]
>>
>> Tom
Mark Rae [MVP] - 06 Sep 2007 09:18 GMT
>> <%=ServerSideControlID.ClientID%>
>
> Where would I put this and how would I use it?

Wherever you need to use methods like getElementById which find a control by
its ID...

var myTextBox = getElementById('<%=MyTextBox.ClientID%>');

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

tshad - 06 Sep 2007 16:53 GMT
>>> <%=ServerSideControlID.ClientID%>
>>
[quoted text clipped - 4 lines]
>
> var myTextBox = getElementById('<%=MyTextBox.ClientID%>');

I have seen this before but am not sure what it does.

What is the difference between:

var myTextBox = getElementById('<%=MyTextBox.ClientID%>');

and

var myTextBox = getElementById('MyTextBox');

Why would the first one get me a span element and the 2nd one not?

Thanks,

Tom
Lit - 06 Sep 2007 17:59 GMT
tshad,

When you give a server control an ID  the client ID is not the same.
ASP.NET adds more to it to be unique.

After you render your web page in the Browser go to the ViewSource and look
for yourself.

The ID is much longer in length because the ASP.NET added  more to it to
make it unique.  This is due to:  what if you have the same user control
multiple times on the same web page.

var myTextBox = getElementById('<%=MyTextBox.ClientID%>');
ASP.NET rendering engine Does the Substitution for what the Client ID is you
don't know it.

var myTextBox = getElementById('MyTextBox');
This is Fixed, you only have the Server Side ID and not the Client Side ID.

Again View the Source in the browser to see how the Server ID is NOT the
same as the Client ID for the Same TextBox or Label or what ever.

Lit

>>>> <%=ServerSideControlID.ClientID%>
>>>
[quoted text clipped - 20 lines]
>
> Tom
tshad - 06 Sep 2007 18:47 GMT
> tshad,
>
[quoted text clipped - 18 lines]
> Again View the Source in the browser to see how the Server ID is NOT the
> same as the Client ID for the Same TextBox or Label or what ever.

Ok,

Here are 2 object.  One asp:Textbox and one asp:Label.  The TextBox is
MyFile and the Label is UploadErrorMessage.

<input name="_ctl0:_ctl1:MyFile" id="_ctl0__ctl1_MyFile" type="File"
style="width:200px" />
<span id="_ctl0__ctl1_UploadErrorMessage" style="color:Red;"></span>

The span has no "name" but both have ids.  Is the "id" the ClientID?

Thanks,

Tom

> Lit
>
[quoted text clipped - 22 lines]
>>
>> Tom
Mark Rae [MVP] - 06 Sep 2007 18:00 GMT
> What is the difference between:
>
[quoted text clipped - 3 lines]
>
> var myTextBox = getElementById('MyTextBox');

The first makes sure that the correct ID is being used. ASP.NET will munge
the IDs of client controls when they are contained within MasterPages or
other UserControls...

Basically, always use the first one and never the second...

> Why would the first one get me a span element and the 2nd one not?

What do you mean by "get me a span element"...? What type of control is
being referenced...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

tshad - 06 Sep 2007 18:49 GMT
>> What is the difference between:
>>
[quoted text clipped - 14 lines]
> What do you mean by "get me a span element"...? What type of control is
> being referenced...?

Well, the one I am having problems with is the span element which is the
asp:Label.  In my view source I see it as:

<input name="_ctl0:_ctl1:MyFile" id="_ctl0__ctl1_MyFile" type="File"
style="width:200px" />
<span id="_ctl0__ctl1_UploadErrorMessage" style="color:Red;"></span>

The UploadErrorMessage is the asp:label.  I assume the "id" is the what I am
trying to get with the <%=...%> format.

Thanks,

Tom
Mark Rae [MVP] - 06 Sep 2007 19:04 GMT
> <span id="_ctl0__ctl1_UploadErrorMessage" style="color:Red;"></span>
>
> The UploadErrorMessage is the asp:label.  I assume the "id" is the what I
> am trying to get with the <%=...%> format.

Yes.

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.