Solved

setting value of label on parent page with javascript

Posted on 2011-02-14
13
516 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now