Solved

setting value of label on parent page with javascript

Posted on 2011-02-14
13
509 Views
Last Modified: 2012-05-11
Hi,

I have test1.aspx and test2.aspx

Test1.aspx simply has an asp:label an asp:textbox and a standard href link.

The link looks like:
<ahref="javascript:;" onclick="window.open('Test2.aspx?label=lblTest','cal','width=530,height=300,left=0,top=0')"><img src="Images/po.bmp" border="0" alt=""/></a>

This opens up test2.aspx fine.

Then on the page load of test2.aspx I have:
  Dim strScript As String = ""
        strScript = "<script>"

        strScript += "window.opener.document.forms(0).getelementbyid(lblTest).innerText = 'gem';"
        'strScript += "window.opener.document.forms(0).txtTest.value = 'gem';"


        strScript += "self.close()"
       
        strScript += "</" + "script>"

        ClientScript.RegisterClientScriptBlock(Me.GetType(), "PopUpwindowOpen", strScript)

As you can see the textbox line is commented out.

This works fine.

If its there on its own, the value gem gets written into the textbox and the page closes.

With the innertext line in though it is erroring.

Basically what I need to know is what is wrong with:
        strScript += "window.opener.document.forms(0).getelementbyid(lblTest).innerText = 'gem';"

to get it working.

The label gets rendered on test1.aspx as:
<span id="lblTest"></span>


Thanks in advance
0
Comment
Question by:scm0sml
  • 6
  • 6
13 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34888116
change it to :
 strScript += "window.opener.document.forms(0).getelementbyid('" & lblTest.ClientID & "').InnerText = 'gem';"

0
 

Author Comment

by:scm0sml
ID: 34888154
yeah but lblTest is test1.aspx whereas the code behind you are refering to is on test2.aspx?
0
 

Author Comment

by:scm0sml
ID: 34888240
Sorry I didn't clearly state that but I am using window.opener so there was a clue in there :)
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34888399
change your link to this :
<a id="lnkOpenWindow" href="javascript:;" ><img src="Images/po.bmp" border="0" alt=""/></a>

in the code behind on test1.aspx put this in Page_PreRenderComplete :
lnkOpenWindow.Attributes.Add("Onclick", "window.open('Test2.aspx?label=' & lblTest.ClientId & ','cal','width=530,height=300,left=0,top=0')"

in the code behind on test2.aspx put this:
strScript += "window.opener.document.forms(0).getelementbyid('" & Request.QueryString("label") & "').InnerText = 'gem';"

0
 

Author Comment

by:scm0sml
ID: 34888572
Hi,

I'm presuming you haven't actually run that code as there are a number of errors in it that is stopping it from running........?
0
 

Author Comment

by:scm0sml
ID: 34888761
Right the script that is getting created is:
<script>window.opener.document.forms(0).getelementbyid('lblTest').innerText = 'gem';</script>

I'm getting:
Message: Object doesn't support this property or method
Line: 15
Char: 1
Code: 0
URI: http://localhost:3376/ImportPayments/Test2.aspx?label=lblTest


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34888860
try changing innerText to Text
0
 

Author Comment

by:scm0sml
ID: 34888919
same error......
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34888955
getelementbyid to getElementById
0
 

Author Comment

by:scm0sml
ID: 34889012
still no joy.

Its rendering as:
<script>window.opener.document.forms(0).getElementById ('lblTest').innerText = 'gem';</script>
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 250 total points
ID: 34890026
Hmm i suspect the problem is that the label on the test1.aspx page is being rendered as a span.

try changing  the following line so there are no single quotes round the lblTest.ClientId :
lnkOpenWindow.Attributes.Add("Onclick", "window.open('Test2.aspx?label=" & lblTest.ClientId & ",'cal','width=530,height=300,left=0,top=0')"

Probably worth testing again at this point.
But I think you will need to change the following line removing the single quotes again and chaning innerText to innerHTML

strScript += "window.opener.document.forms(0).getElementById(" & Request.QueryString("label") & ").innerHTML = 'gem';"

This link may be of some help too :
http://www.htmlgoodies.com/beyond/javascript/article.php/3686901/How-to-Populate-Fields-from-New-Windows-Using-JavaScript.htm
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 34896146
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35016648
The question has been answered in good faith, you can't just delete the question when experts have spent time helping you
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now