开发者

window.opener.document.getElementById not working

In my asp.net web app, I create a popup window with a button. When that button is clicked, I want to set the value of an asp:TextBox (id=TextBox1) contained in the parent window. However, it doesn't work like all the examples I've read indicate.

I've tried the following lines of code in my javascript onclick handler:

  1. window.opener.document.getElementById('<%= TextBox1.ClientID %>').value = "abc";
  2. windo开发者_如何学Cw.opener.document.getElementById("TextBox1").value = "abc";
  3. window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox1").value = "abc";

Only example 3 works. All the stuff I've read indicates that #1 is the preferred method, but I can't seem to make it work at all. Does anyone have any ideas what I'm doing wrong?

I've tried this in Firefox, Chrome and IE.

Thanks


you need to change a bit when calling your popup code , you have to pass your textbox client id and then you can set its value from popup page without any hard codding. here is the way :

var txtNameClientObject = '<%= txtName.ClientID %>';
window.open('Child.aspx?txtName='+txtNameClientObject);

and then in popup page you can do it as

opener.document.getElementById('<%= Request["txtName"] %>').value = 'from child';

hope this will be helpful for you.

Thanks


Is this line of JavaScript contained in the markup for the popup window itself? If so, the server-side code for that won't be aware that TextBox1 exists on the server-side code for the parent window, and won't be able to determine its ClientID property. You either need to pass that client ID to the popup window somehow (querystring, cookie, session, whatever) or hard code it. Alternatively, you may be able to put this line of JavaScript in a function on your parent page, and then call something along the lines of window.opener.functionName().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜