ASP.NET Refresh Parent Page after Closing Modal Dialog Window - asp.net

I have an ASP.NET page that launches a modal dialog window:
Dim sURL As String = System.Configuration.ConfigurationManager.AppSettings("PAYORS_Path") & "PayorCopy.aspx"
lnkCopy.Attributes.Add("onclick", "javascript:window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');return false;")
The user can create new items in this modal window. When the user clicks the Close button the modal dialog window is successfully closed, but the new items created do not appear on the parent page. When the modal window is closed I would like the parent page to refresh to show the new items the user created. Currently:
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnClose.Click
Dim strjscript As String = "<script language='javascript'>self.close();</script>"
LtClose.Text = strjscript
End Sub
I have tried adding to this script:
window.opener.location.reload(true);
But then when I test I get an error that says "Unable to get value of the property 'location': object is null or undefined."
Any help would be much appreciated!

Instead of using the old modal dialog window, try jQuery Dialog because it behaves just like a modal but keeps you in the same window. Therefore it's is easily refreshed with the following code:
location.reload(true);
For more information, check out this question here at SO.
Hope it helps.

That would be because there is no 'opener' :)
Location is method of the window object so you can even treat it as global is most cases. Any of those should work:
window.location.reload(true);
or:
location.reload(true);
EDIT Yeah I just noticed where you put that script.. if it's in the click handle inside the modal..then 'window' there is really a different one - it's modal after all! Try adding it directly after the code opening modal window - the script execution will be blocked until the modal returns, so you can so something like this:
var shouldReloadPage = window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');
if(shouldReloadPage){
window.location.reload(true);
}
return false;
This is still inside the click handler ofc, just showing in code block for easier reading.

Related

On Asp.net Button click open link in new maximized window

I have a button in Update panel in my page. on button click event dependendant on the text boxes entered i m buliding an url and have to open that url in new window.
i have tried:
i) window open .. but it opens un maximized , not a fully maximized window. i tried setting its height to screen hieght,,but the requirement is full window only, shouldnt need to be maximize every time i open window.
<Asp:button OnClick="Btn1_Click" />
//in code behind
protected void Btn1_Click(object sender, EventArgs e)
{
//my code to generate url
// In below lines im trying to open window
//Im ont using OnClientClick becoz..the button is in Update panel
ScriptManager.RegisterStartupScript(btnLoadReport.Page,
Btn1.GetType(), "", "window.open('" + myUrl+ "','','height=' + screen.height + ',width=' + screen.width + ',resizable=yes,scrollbar=yes,toolbar=yes,menubar=yes,location=yes,top=0, left=0');", true);
}
or can i create a hyperlink click event dynamically on button click event.. and how do i try that?
any ideas and samples would be appreciated.
Thanks,
Ahmed
try this: OnClientClick="window.open('url')"
the c# file you write,
button.attributes.add("onclick","javascript:function1()");
in the aspx page,write the below javascript function
function function1()
{
window.open("pagename",other attributes whose values are to be passed to other page);
}
Did not understand you the first time. Try this. Im guessing your not using update panels here right? cause if you are you should use scriptmanger.register.. instead of client script
Btn1_Click()
{
//my code to bulid url
ClientScript.RegisterStartupScript(this.GetType(),"Redirection","window.open('www.google.com', '_blank')",true);
}
<Asp:button OnClick="Btn1_Click" />
try this please : window.open(url, '_blank');
EDIT : look at this link dude example for opening new tab
My issue got resolved by adding 'chromemode=yes' in parameters.. like below..
window.open(url, 'chromemode=yes, scrollbars=yes' );
thank you all for your answers :)

Denying subwindow change in QMdiArea

I have a QMdiArea with several QMdiSubWindows. While construction, the QMdiArea is initialized as TabbedView and DocumentMode, so it looks like a QTabWidget by default.
When user clicks the tab to switch the sub windows, I want to add a confirmation, and if the user chooses cancel, the active tab will not be changed.
My solution is to handle the signal "subWindowActivated", and calls "setActiveSubWindow" back to the original sub window if the confirmation fails.
The problem is, the setActiveSubWindow call didn't work as expected. The active window is switched back to the original one, but the sub windows are no longer maximized, but overlapped. And the original sub window appears behind the user clicked one. I can call setWindowState to maximize the sub window, and set focus to it, but the sub window is still behind the user clicked one.
I want to know if there is proper way to switch back to the original sub window after user clicked a new one.
Below are some code pieces
MyClass::MyClass()
{
m_pMdiArea = new QMdiArea;
m_pMdiArea->setViewMode(QMdiArea::TabbedView);
m_pMdiArea->setDocumentMode(true);
connect (m_pMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow *)), this, SLOT(subWindowActivate(QMdiSubWindow *)));
}
void MyClass::subWindowActivate(QMdiSubWindow * window)
{
if (!window || window == m_pCurrentWindow)
{
return;
}
if (ConfirmationRejected)
{
m_pMdiArea->setActiveSubWindow(m_pCurrentWindow);
return;
}
m_pCurrentWindow = window;
}
You should use at Cancel block:
activatePreviousSubWindow ();

Anguilla - Updating a field's value from a popup?

I have a modal popup that appears whenever an editor tries to save a component with some values (a date field in the past in this case).
In this popup I show the editor a few options (very similar to the default "Open Shared Item" dialog) and an OK/Cancel button combo. On Cancel I fire the "cancel" event and the editor goes back to the editing screen, all good here. On "OK" I want to change the value of the field to match whatever the editor selected, then save.
I tried to use an approach with FieldBuilder and the sample Boris mentioned on this other topic but I can't get to the field from my popup dialog.
Any suggestions on how I can go and modify the xml of the item (could be also a page) from a modal popup?
EDIT: Code used in getControlForFieldName
function getControlForFieldName(name) {
var fieldBuilder = $display.getView().properties.controls.fieldBuilder;
var fieldsContainer = fieldBuilder.properties.input;
var fieldsNode = fieldsContainer.getElement();
var fieldContainer = $dom.getFirstElementChild(fieldsNode);
while (fieldContainer) {
var labelNode = $dom.getFirstElementChild(fieldContainer);
var fieldNode = $dom.getNextElementSibling(labelNode);
var control = fieldNode.control;
if (control.getFieldName() == name) {
return control;
}
fieldContainer = $dom.getNextElementSibling(fieldContainer);
}
}
EDIT #2
After Frank's advice, and some help from Jaime & Frank offline, I got it to work as follows:
The popup is called from a Command Extension (Save & Close in my case)
The command.js specifies an event handler that gets called on "submit" (== OK was pressed)
$evt.addEventHandler(p.dialogPopup, "submit",
this.getDelegate(this._onPopupSubmit));
In my popup I am passing the selected item (it's a keyword ID) to the event handler:
this.fireEvent("submit", { id: select.options[select.selectedIndex].value });
and now back in the event handler _onPopupSubmit(e) I just read e.data.id, load this keyword, get properties like ID & Title, and update the metadata of the item using item.setMetadata("new metadata with updated values").
Simple :)
Your code runs in a popup, so any references you make to global variables will be taken from the popup window.
So when you get the fieldBuilder:
var fieldBuilder = $display.getView().properties.controls.fieldBuilder;
$display is a reference to a global variable. So this actually looks for the FieldBuilder in the popup window (which doesn't have one).
To get the FieldBuilder of the Component window, you can get it from the opener:
var fieldBuilder = opener.$display.getView().properties.controls.fieldBuilder;
You might want to consider actually passing the updated value to either a callback function or with a (custom) event though, since that makes your popup less dependent on opener. trick.

How can I execute JavaScript from my code-behind after my UpdatePanel has finished loading its DOM elements?

I have an UpdatePanel with a repeater in it that is re-bound after a user adds an item to it via a modal popup.
When they click the button to add a new row to the repeater the code-behind looks something like this:
protected void lbtnAddOption_Click(object sender, EventArgs e)
{
SelectedOption = new Option()
{
Account = txtAddOptionAccountNumber.Text,
Margin = chkAddOptionMargin.Checked,
Symbol = txtAddOptionSymbol.Text,
Usymbol = txtAddOptionUsymbol.Text,
};
Presenter.OnAddOption(); // Insert the new item
RefreshOptions(); // Pull down and re-bind all the items
mpeAddOptionDialog.Hide(); // Hide the modal
// ... Make call to jQuery scrollTo() method here?
}
This works fine and the new row will show up quickly via the UpdatePanel.
However, there are often hundreds of rows and where the new one is added is based on the current sorting column used.
So, I wanted to take this as a chance to use the sweet jQuery ScrollTo plugin. I know that if I give it the ID of my overflowed container div and the ID of an element within it, it will smoothly scroll straight to the users newly added row.
However, there are two problems:
I need to find the appropriate row so I can snag the ClientID for it.
I need to execute the jQuery snippet from my code-behind that will cause my newly updated repeater to scroll to the right row.
I've solved #1. I have a reliable method that will produce the newly added row's ClientID.
However, problem #2 is proving to be tricky. I know I can just call ScriptManager.RegisterStartupScript() form my code-behind and it will execute the JavaScript on my page.
The problem I'm having is that it seems that it is executing that piece of JavaScript before (I'm guessing) the newly refreshed DOM elements have fully loaded. So, even though I am passing in the appropriate jQuery line to scroll to the element I want, it is erroring out for me because it can't find that element yet.
Here is the line I'm using at the end of the method I posted above:
string clientID = getClientIdOfNewRow();
ScriptManager.RegisterStartupScript(this, typeof(Page), "ScrollScript", String.Format("$(\"#optionContainer\").scrollTo(\"{0}\", 800);", clientID), true);
What do I need to do so I can ensure that this line of JavaScript isn't called until the page with the UpdatePanel is truly ready?
If the stuff you need to process is in the update panel, then you need to run your JS once that panel is loaded. I use add_endRequest for that. This below is hacked from something rather more complex. It runs once on document ready, but installs the "end ajax" handler which is triggered every time your update panel is updated. And by the time it fires, it's all there for you.
var prm = Sys.WebForms.PageRequestManager.getInstance();
jQuery(document).ready(function () {
prm.add_endRequest(EndRequestHandler);
});
function EndRequestHandler(sender, args) {
// do whatever you need to do with the stuff in the update panel.
}
Obviously you can inject that from code-behind if you want.
You can use the Sys.Application.load event which is raised after all scripts have been loaded and the objects in the application have been created and initialized.
So your code would be:
string clientID = getClientIdOfNewRow();
ScriptManager.RegisterStartupScript(this, typeof(Page)
,"ScrollScript"
,String.Format("Sys.Application.add_load(function(){{$(\"#optionContainer\").scrollTo(\"{0}\", 800);}});"
, clientID)
, true);

ASP.NET validation not executing in a JavaScript showModalDialog call

I currently open a pop up window from my parent page using using a JavaScript .showModalDialog function. The pop up window contains some ASP.NET validation controls which do not display when the user clicks the ASP.NET button to submit the form. If there is an error on the page, the validation message(s) do not display, the record is not updated on the server side and the pop window closes.
(The asp.net validation controls do not stop the pop up window from doing a server postback)
Has anyone expereinced this behavior before and is there any way to prevent it?
Here is my showModalDialong call source code:
function OpenChildWindow(id)
{
var sFeatures = sFeatures="dialogHeight: 525px;";
sFeatures += "dialogWidth: 900px;";
sFeatures += "scroll: yes;";
sFeatures += "status: no;";
sFeatures += "resizeable: no;";
var url = "MyPopUp.aspx?ID=" + id;
var childName = "ChildForm";
entryWindow = window.showModalDialog(url, childName, sFeatures);
if (entryWindow == true)
{
window.document.getElementById("<%= btnUpdateParent.ClientID %>").click();
}
}
Note: When the pop up modal is closed, a ASP.NET button is "clicked" to update an ASP.NET UpdatePanel on the parent to show the changes to the record modified in the pop up window.
I think this may be due to a notorious problem with modal dialogs and postbacks. You can try adding the following in the head tag of the page which you open with window.showModalDialog
<base target="_self" />

Resources