Solved

setting value of label on parent page with javascript

Posted on 2011-02-14
13
521 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Optimize the query 5 42
Name Space error VS2015 1 22
DataGridView / get bound table name? 8 24
JavaScript Sort Ordering and Choice Limiting 5 17
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'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

791 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