How to use JavaScript variable from popup window in code behind? - javascript

I have ASP.NET web application that contains master page. Master page contains content page. Content page contains user control. User control contains Telerik grid with its context menu.
I'd like to click the item in grid's context menu and open new popup modal window. In that window there's drop down list. I pick up some option from drop down list and click OK. I'd like to get the value selected from drop down list and use it in my ASP.NET code to proceed further.
I've tried to use hidden field to store the value from drop down list but it doesn't work because I'm not sure where hidden field should be placed.
This is my code:
Open popup window:
function ClientItemClicked(sender, eventArgs)
{
if (eventArgs.get_item().get_value() == "excel")
{
var retVal = window.showModalDialog("ExportToExcelChoice.aspx", null, "dialogWidth: 400; dialogHeight: 200; center: yes; resizable: no;");
}
}
Click "OK" to close popup window:
function ReturnValue() {
var choice = document.getElementById("DropDownList1").value;
if ((window.opener != null) && (!window.opener.closed)) {
window.opener.document.getElementById("HiddenField1").value = choice;
}
window.close();
}
It fails on this line:
window.opener.document.getElementById("HiddenField1").value = choice;
Because hidden field is placed in user control and the code can't get the reference to hidden field.
Could someone help me to make it work?

If you're using window.open(), you can see into the parent window via the property window.opener, which will let you communicate between your parent page and the popup.
If you're using window.showModalDialog(), see the second answer to this question: window.opener alternatives

Try this
window.opener.document.getElementById('<%= HiddenField1.ClientID %>').value = choice;

Related

aspx/c# Refresh on closing popup

I have a little problem with my code.
I have an asp:Wizard element, in which I have few steps.
In one step, I click on a button that open a popup with window.open to select a datetime.
When I selected one, the popup is closing, but the datetime is not visible in my parent window (but it's there, because when I click again on my window.open it's "reloading" my page and the date is now visible)
I already tried to reload my parent's page when I close the popup with window.parent.opener.location.reload but this solution make me lose the active step of my wizard.
So what I need is a partial refresh of my page that keeps the current step.
below is the code
My opener:
var childWindow = window.open("../../Utils/CalendarPopup.aspx?DatePred=Stateme‌​‌​ntDateFrom", "", "height=280; width=285;);
childWindow.onunload = function ()
{ // Where i need to reload }
My popup:
Session[Request.Params["DatePred"]] = CalendarSelectDate.SelectedDate;
Session["CalendarPopupCanceled"] = 0;
this.ClosePage();
The data loading:
if (Session["StatementDateFrom"] != null)
{
{
(WizardProcess.FindControl("DtTxtBxStatementDateFrom") as Syncfusion.Web.UI.WebControls.Shared.DateTimeTextBox).IsNullDate = false;
(WizardProcess.FindControl("DtTxtBxStatementDateFrom") as Syncfusion.Web.UI.WebControls.Shared.DateTimeTextBox).Value = System.Convert.ToDateTime(Session["StatementDateFrom"]);
}
Session["StatementDateFrom"] = null;
}
Thanks in advance
i don't know how you wrote your code, but why you need to reload the child window. If you have a control (for example a textbox) that displays the datetime you selected from the childWindow, then you can set the control's value equal to the selected datetime. This should be easier than trying to refresh only the wizard!

Firefox extension: get click data when context menu

In my extension I want to hide/show items on the context menu based on the url I get:
from a link if the user opens the context menu over one,
from the text selected, also if the user opens the context menu over the text.
In the function to show/hide items in the context menu I do the following check:
if (gContextMenu.onLink) {
url = gContextMenu.target.href;
}
if (gContextMenu.isTextSelected) {
url = content.window.getSelection();
}
If some text is selected in the page, and the user opens the context menu over a link, both conditions are true. Also, if some text is selected, and the user opens the context menu anywhere in the page (over the selection or not), the isTextSelected flag is also true.
Is there a way I can detect what is the real element over which the user has used the right click? How can I know if the right click was over the selected text or not?
First of all, there is gContextMenu.target, which contains the Node which was under the cursor, if any (aka. it might be null in very rare cases).
The gContextMenu code actually uses gContextMenu.target to initialize things like .onLink
As for checking if the the focused node is actually contained within the current selection, you can use .getSelection().containsNode() on the focused document.
var isFocusedNodeInCurrentSelection = false;
var selection = gContextMenu.focusedWindow &&
gContextMenu.focusedWindow.getSelection();
if (selection) {
isFocusedNodeInCurrentSelection =
selection.containsNode(gContextMenu.target, true);
}
// Do something with that information.

Setting a serverside session object in javascript in asp.net

I searched SO and google, but did not find anything that would resolve my issue.
I have a popup window where the user can click "Save", "Delete" or "Close". Each of these three functions will eventually close the popup window. On the parent window codebehind, I need to know which of the 3 buttons the user clicked.
Currently, on the popup window (if the user clicks Delete), I have (pre-existing code) :
function CloseDelete() {
var agree = confirm("Are you sure you want to delete this record?");
if (agree)
__doPostBack("<%=btnDelete.UniqueID %>", "");
// create session object here //
else
return false;
GetRadWindow().Close();
}
I was thinking of creating a session object in javascript on the popup window, which I could check on the parent window codebehind. How would I create a session object in the function above ? Or how can I determine which button the user clicked?

JavaScript: How to reference user control inside content page?

This is structure I have:
Master page -> Content page -> User control -> Telerik grid with its context menu, hidden field.
This means: Master page contains content page, content page contains user control, user control contains Telerik grid with its context menu and hidden field.
I open popup window by clicking option in Telerik grid's context menu. After I choose some option in combo box in that popup window I press OK and close it. But I don't know how to reference opener that should be user control with Telerik grid and hidden field. I want to set hidden field to some value.
This is JavaScript code I use:
<script language="javascript" type="text/javascript">
function ReturnValue() {
var choice = document.getElementById("DropDownList1").value;
if ((window.opener != null) && (!window.opener.closed)) {
window.opener.document.getElementById("HiddenField1").value = choice;
}
window.close();
}
</script>
But, it fails on this line because opener is master page:
window.opener.document.getElementById("HiddenField1").value = choice;
So, how can I make it work?

How to refresh parent browser window on GridView page change

I'm using ASP.net; I have a popup browser window that contains an databound gridview with textboxes. It has an "Add to Order" button which takes the values entered and updates the database, then closes the popup and refreshes the parent. This currently works perfectly using window.opener.document.forms[0].submit();self.close(); in a RegisterScriptBlock
I now need to update the database on gridview page chage so that textbox values are not lost. I put window.opener.document.forms[0].submit(); into the PageIndexChanging event of the datagrid, but it does not refresh the parent window. Refreshing the parent window with the order lines helps the user see what they have already ordered. My update database method runs fine, just not the parent browser refresh. I also tried "window.opener.location.href = window.opener.location.href" to no avail.
Thank you in advance!
In parent aspx page write one JavaScript function
function fnReload() {
alert('hi')
window.location.href = window.location.href;
}
Protected Sub grdDisplay_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdDisplay.PageIndexChanging
Dim strScript As New StringBuilder()
Call addItems()
grdDisplay.PageIndex = e.NewPageIndex
Call search()
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "reload", "window.opener.fnReload();", true);
End Sub
Also insert on alert in fnReload() function to check that this function called or not.

Categories

Resources