I have a form with textbox. Besides this textbox i should have imagebutton / something which on clicking will open a popup window (lookup). This popup window will have UserId and Usernames (from db). So when the User selects one from this , the UserId should get selected in the textbox. How to do this? Please help. Can you please send me a sample of this type ..? Its very urgent.
Thanks lot..

i use the following code but its not working

to open popup

string servicename = ((Button)sender).Text.ToString();       
        string url = "Postcode.aspx?Websevice=" + servicename;
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(),"str", "<script language='javascript'>window.open('" + url + "','height=auto,width=1000,scrollbars=1,top=250,right=20');</script>", false);

this will fine open new user window

now i set selected variable of user list in parent page

string id = ViewState["Id"].ToString();
        string textid = "textbox5";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "str", "<script language='javascript'>window.opener.document.getElementById('" + textid + "').value ='" + id + "'; window.close();</script>", false);

here id is selected value and text id is parent page textbox id in which i want to pass value.

Dani AI

Generated

is right that window.opener is the right mechanism. The real problem in ’s code is almost always one of these: the parent control’s rendered client id is different from the server-side id, the script is registered at the wrong time (UpdatePanel/async postback issues), or the JavaScript string is not escaped. A simple, robust pattern avoids guessing IDs in the popup: define a small callback on the parent and let the popup call it.

Parent page — define a client callback that uses the server control’s ClientID:

<script type="text/javascript">
function userPicked(id, name) {
  var tb = document.getElementById('<%= txtUserId.ClientID %>');
  if (tb) { tb.value = id; /* optionally trigger __doPostBack or other logic here */ }
}
</script>

Popup page — call the parent callback and close:

if (window.opener && !window.opener.closed && typeof window.opener.userPicked === 'function') {
  window.opener.userPicked(selectedId, selectedName);
  window.close();
}

If a parent-side JS function isn’t desirable, pass the parent control’s ClientID when opening the popup and use it in the popup to locate the element. When registering scripts from server code, prefer ScriptManager.RegisterStartupScript (especially inside UpdatePanels) and always JavaScript-encode values (HttpUtility.JavaScriptStringEncode) so quotes/backslashes don’t break the generated script. Also verify same-origin (protocol/host/port), check browser console for errors, and remember setting the DOM value does not persist to server state until a postback or AJAX update.

Recommended Answers

All 3 Replies

Use window.opener javascript method.

I already used it in my code see my code but i can't get value can u give me a working example. Thanks a lot for reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.