Window.Opener and referencing by ID issue because of .NET control - asp.net

I have a .aspx page that has a link on it, then when clicked opens a new window using window.open.
I need to send a integer back and put that number into a textbox (which is a .NET control).
When I call window.opener on the popuped up window, I have to reference the ID of the textbox. The issue is, the ID changes from time to time if you add things to the control tree.
How can I reliably reference the textbox's ID from the new window?
I have jQuery installed also, but not sure if I can use jQuery from the new window?

Instead of accessing the element directly from the popup, put a function in the page that the popup can call. In the function you can insert the actual id of the element:
function setTextbox(value) {
document.getElementById('<%=TheTextBox.ClientID%>').value = value;
}
In the popup:
window.opener.setTextbox("Hello world!");

This should work
// original window script
var windowHandle = window.open(...);
windowHandle.top.otherWindowTextBox = document.getElementById('idOfTextBox); // or use jQuery
Now in you popup window, you have a reference to your textbox on the page that opened the popup window.
// script in popup window.
top.otherWindowTextBox.value = someInteger;

Related

C#: How to Re-RegisterClientScripts?

I have a problem which I can't find a solution. I have a Parent page calling dynamically a default user control ( 6 different UC based on a selected item Combo X on the parent page). Within the UC, i inject (registerClientscript) a javascript which controls visibility within the UC based on a combo box. All UC has the same combo but the controls within this UC can vary.
The problem i am having is that on first load, the JS is generated correctly... but on change of Combo X from the parent, i trigger a partial refresh of the UC, which in turn re-register a new JS.
function DefineView(sender, eventArgs) {
var comboSearch = $find('%%cmbSearchType%%');
//cmbSearch Section
switch (comboSearch.get_selectedItem().get_value()) {
[[MY CODE HERE]
}
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "SearchVisibilityPPSA",jsFunction.Replace("%%cmbSearchType%%", cmbSearchType.ClientID),true);
As you can see, I replace the ClientID, and on first load of the page, this is resolved correctly like ctl00_PrincipalPlaceHolder_ctl00_cmbSearchType but when I change Combo X, it reloads the user control, which in turn reload the script above. In the rendered HTML, The COmbo ID is renamed to ctl00_PrincipalPlaceHolder_ctl01_cmbSearchType (Note the subtle change in name from ct00 to ct01 ) In my debug, I saw this ClientID contain the new ID but somehow it is not replaced regenrated on the rendered html.
I guess my question is how do i force the JS to be re-rendered every time this UC is called? For some reason, it is always using the original rendered JS ( which is why it is working the first time)
I think this is related to my dynamic control i was generating without assigining any id ... by forcing the id attribute, it kept it the same...

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:
window.opener.document.getElementById('<%= TextBox1.ClientID %>').value = "abc";
window.opener.document.getElementById("TextBox1").value = "abc";
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().

Get the active index of the jquery accordion pane from asp.net on the server-side?

How can I get the active index of the jquery accordion pane when a button is clicked? What I want to do is when a button is clicked in pane 3 for example, I want to store that value and when the page is reloaded, I want pane 3 to remain open.
I intially had this in my server side click and when I hard code a value in for paneIndex it works fine, but obviously, I don't want to do this, I want to get the index on the click and pass that to the script.
string script = "<script type=\"text/javascript\">var paneIndex = " + 3 + "</script>";
if(!ClientScript.IsStartupScriptRegistered("JSScript"))
ClientScript.RegisterStartupScript(this.GetType(),"JSScript", script);
You could store the value in a hidden form field, and assuming you are doing a postback, that information will now be in the hidden field for you to use on the server side.
You will want to bind a function to the change event of the accordian, and store the new active index into a hidden input so that it gets sent back to the server.
As far as round-tripping the active index back to the HTML that is returned from the server - your approach is fine. Obviously instead of the hardcoded value of 3, you would put in the value from the hidden input.
$("#accordion").accordion({
active:1,
change: function(event, ui) {
var activeIndex = $("#accordion").accordion('option','active');
$("myHiddenInputId").val(activeIndex);
//alert(activeIndex);
}
});
From the server side, you can access the value and push it back to the page in a similar manner as you posted in the question:
string activeIndex = Request.Form["myHiddenInputName"];
string script = string.Format(#"<script type=""text/javascript"">var paneIndex = {0};</script>", activeIndex);
That should do it.
What's also a possibility is using the jquery.cookie plugin and storing the active pane index in a cookie. That way, everything actually happens clientside.
I don't know if this might be a valid answer, just throwing it out here for completeness sake :D

Passing a value from child window to parent

Here is my scenario: i have a parent web page, in my javascript code I open a new window. This new window is a server-side aspx page. This child page needs to save some data in the database, and after saving it returns an ID. Now, I need to pass this ID from the child server page to the parent's javascript.
Essentially I need the child server code to trigger:
1) return the value to the javascript code of the parent page
2) close itself
What would be the acceptable way to do it?
Set a variable in the parent page:
var dbID;
When the DB call returns from the opened window, place this in the opened window's load event (or ready event if using jquery or another framework):
window.opener.dbID = <%= newID %>
// or whatever your framework's technique is for this :-)
window.close();
You can also call a global function on the parent as well using the same technique.
On "parent" page:
function handleNewDBID(dbID) { alert("lookit me, i got an ID! " + dbID); }
And on the opened window:
window.opener.handleNewDBID(<%=newID%>);
If you are in the child window AND assuming your parent window has object childResult:
window.opener.childResult = "yourIdHere";

ASPnet web Form Navigation

I want to redirect a new tab and to get focus of the new window when a button is clicked.. I can create a new window, but can't get its focus even thourh, I tried the following code
Window.focus(); How to do this?
My Code:
function new_window(url)
{
//Open a Window in New tab
var popupwin = null;
popupwin = window.open(url);
popupwin.focus();
self.focus();
//window.focus();
}
I think it would be better not to do this. These are browser preferences and don't try to override those. It may fail due to user settings.
Remove
self.focus();
You can use focus() method of a form element. This brings mostly the window to front. window.focus() might implemented different by different browsers.
Do you have html input elements on you popup win?
Try calling focus() on one of the html input elements. This will place the cursor into the element to assist the user start typing there.

Resources