how to refresh parent window by using Javascript in .cs - c#

How to refresh the parent window while closing the popup window(child window).
We are calling the java script functions in code behind to refresh the parent window by using page.ClientScript.RegisterStartupScript().But t is working fine in IE(internet explorer) but not working in Mozilla Firefox and Google Chrome.
In the Mozilla Firefox the pop up value is saving in the database but it is not updating into the parent page.If i did refresh manually the value is getting updating into the parent page. If i put debugger in RefreshPage()(javascript function) function in IE it is firing but not in Firefox.
The below code for call the javascript function in .cs class.
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", "<script>javascript:alert('" + dsMessage.Tables[0].Rows[0]["ErrorMessage"].ToString() + "');window.open('','_self','');window.close();window.document.forms[0].submit();</script>");
The above code RefreshPage() is the javascript function to refresh the page
i.e.
function RefreshPage() { window.document.forms[0].submit(); }
Please help me i tried with different scenarios but no output.
instead of RefreshPage() i used different functions
like reload(),
window.opener.forms[0].submit(),
likewise but still no output anyone knows please help me.

Try this function
on submit button click run this script
<script language='javascript'> window.opener.frames.location='somepage.aspx';window.close();</script>
this will help you !!!

Before trying to make js calls between windows.
Set 'document.domain' to the same value on both pages.

protected void ButtonClick(object sender, EventArgs e)
{
string closeAndRefreshScript = #"<script type='text/javascript'>
window.opener.location.reload();
window.close();
</script>";
base.Response.Write(closeAndRefreshScript);
}

Related

asp.net change/write html to page before pageload event (c sharp)

I have some long code on server side that runs on Page_Load(). My problem is that users don't see nothing until this code is over
I want to show them some loading picture so that they will know that something is happening. I tried to put some JavaScript alert() on page but it didn't helped. I tried to changed html from server-side but of course it only shows the changes after job is done.
protected void Page_Load(object sender, EventArgs e)
{
// show user a loading image
// start heavy coding
}
You can use AJAX or UpdatePanel to separate loading and user-visualizing processes.
In this case you just need pageLoad() - js function, which will call proper, for example, web-service-method and shows loading image. OnSuccess\OnError events if ajax call will help you to handle the result of this call

Calling a Javascript function from .cs

I have an Update panel where a button and a grid are added as follows: this._UpdatePanel.ContentTemplateContainer.Controls.Add(this._BtnSave); this._UpdatePanel.ContentTemplateContainer.Controls.Add(this._AssignGrid);
On click of this button I am calling a function which resides in a different class AssignedGrid.cs.
this._AssignedGrid.getSelectedRows();
AssignedGrid.cs inturn calls a javascript function as follows:
public void getSelectedRows()
{
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "GetSelectedItems", "<script type='text/javascript' language='javascript'>GetSelectedItems();</script>");
}
But this javascript function is never called! Please let me know if I am calling the function the proper way.
This should work,
ScriptManager.RegisterStartupScrip(this.Page, this.Page.GetType(), "GetSelectedItems", "GetSelectedItems();", true);
The first parameter of the Script manager expects a page object (http://msdn.microsoft.com/en-us/library/bb310408(v=vs.110).aspx)
This is probably a user control, web control, etc and not an actual page object, as such it doesn't work. The only time this works is if you are in the context of a page, e.g. like a class that inherits from page.
So just send it the page object explicitly.
Once you have that, if it's not getting called, use a browser like Chrome, open the developer tools (f12) and go to the console window and look for javascript errors. If it was called but wasn't found you should see "GetSelectedItems does not exist" or a similar error. And that would mean your function is being declared and loaded after the code that calls it was inserted in the dom order by the ScriptManager.
The ScriptManager will insert the code call at the end of the page before the close tag, so define GetSelectedItems in the tag or somewhere above it.
You dont have to give script tags
ScriptManager.RegisterStartupScript(this, GetType(), "GetSelectedItems", "GetSelectedItems();", true);

How to close a modal window after a save in Asp.NET WebForms

I have some javascript code on my main page that opens a popup when a link is clicked on:
<script language="javascript">
function OpenResidentialAddressWin(subscriberContactRelationGid, routeId, btn) {
window.showModalDialog("SubscriberResidentialAddress.aspx" + BuildQueryStringValuesForSubscriber(subscriberContactRelationGid, routeId, returntxtReceiptDate().value), this, strWindowFeatures + ";scroll:no;dialogWidth:442px;dialogHeight:350px");
location.href = location.href;
}
</script>
So the above code opens up the page SubscriberResidentialAddress.aspx in a modal window (as well as passing a number of variables into this page)
Now this page is arranged in such a way that it either displays or edits information. (These are in separate divs that are displayed or hidden dependign on what buttons have just been pressed in the window).
Currently after a user makes a save to the information on the page it switches to display mode. Instead I want to close the modal window completely and reload the main page.
A colleague suggsested that I use a literal control on the aspx page of the modal window and write javascript to it after a successful save has been carried out. So I placed the following in the head section of the SubscriberResidentialAddress.aspx page:
<asp:Literal runat="server" ID="litCloseWind"></asp:Literal>
And then in the code behind in the function that is called when a save is carried out
protected void btnAddSave_Click(object sender, EventArgs e)
{
////// code related to saving
if (status.IsSuccessful)
{
litCloseWind.Text = "<script>this.close()</script>";
Response.Redirect(Request.Url.AbsoluteUri, false);
}
}
I have tried the above and also litCloseWind.Text = "window.close()"; but neither were successful in closing the modal window after a save is carried out.
Is this plan logically sound and have I just made an error somewhere or was this a bad way of doing it to begin with?
Why don't you register a script here, like this:
ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
No need of putting any Literal Control here.
So your code will look something like this:
protected void btnAddSave_Click(object sender, EventArgs e)
{
if (status.IsSuccessful)
{
ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
}
}

executing javascript in awesomium to click on a div

I'm using Awesomium in C# winforms app to click on a div inside the webpage.
the following script works just fine if you type it directly in google chrome:
javascript: document.getElementById('ac_play').click();
but when I try to execute it in awesomium using either:
webControl1.ExecuteJavascript("document.getElementById('ac_play').click();");
or this:
webControl1.LoadURL("javascript: document.getElementById('ac_play').click();");
It's not working. Which makes me think - does Awesomium support "div clicks" or not? Or maybe there is another reason why it's not working?
I've also tried to execute the code:
on LoadCompleted event (to make sure that the page is loaded completely)
by typing the script manually in "addressBox1" (native awesomium "address bar" control)
As usual - nothing is working.
EDIT:
I've tested the same javascript in GeckoFX and it's not working there either.. Any workaround?
EDIT2
Standart WebBrowser control executes the script perfectly! (Although it's using IE5 or so.. that's why I'd like to see Awesomium solution working).
P.S. it was working in webcontrol and now it's not ;( how do i reset it?
P.P.S. i removed the cache in IE but it's sooo unreliable to use standard WebBrowser..
Remember that you have to wait for DocumentReady event with DocumentReadyState as Loaded (not Ready - because then Awesomium is not quite ready yet)
private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
{
if (e.ReadyState != DocumentReadyState.Loaded) return;
//Here ExecuteJavascript should work
}
public void JsFireEvent(string getElementQuery, string eventName)
{
webControl1.ExecuteJavascript(#"
function fireEvent(element,event) {
var evt = document.createEvent('HTMLEvents');
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
element.dispatchEvent(evt);
}
" + String.Format("fireEvent({0}, '{1}');", getElementQuery, eventName));
}
JsFireEvent("document.getElementById('ac_play')", "click");
Link

How to load javascript function from an dynamicall added control?

The Master page got a ScriptManager.
Then i got a Control with a ScriptManagerProxy and an UpdatePanel.
Inside the UpdatePanel there I dynamically add a Control (also containing a ScriptManagerProxy) and from that control I need to run some JavaScript code.
DynamicControl.ascx:
<script type="text/javascript">
function doSomething() {
alert(1);
}
</script>
DynamicControl.ascx.cs:
public void Page_Load(object sender, EventArgs e)
{
...
ScriptManager.RegisterStartupScript(
this.Page, this.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
My problem is that the function "doSomething()" is never called and i dont know why. :S
Edit: It is called but not directly when i add the control.
If I do code like this, there will be an alertwindow:
"<script type='text/javascript'>alert(1);</script>"
Ok I think i need to add some more information:
The control is dynamical added inside a jQuery Dialog. And I found out that the javacode is first executed after i close and then open the dialog. Some kind of event is triggered so that the code is executed there i think. Is it possible to force this event? so the scripts are executet directly when the control is added ?
placeHolder.Controls.Add(dynamicReportControl);
This c# code doesn't execute the javascript tag immediately ?
Please try something like this
ScriptManager.RegisterStartupScript(
this.UpdatePanel1, this.UpdatePanel1.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
Where UpdatePanel1 is your UpdatePanel
Try setting the addScriptTags argument to true, and remove the script declaration from your code.
Is that code only supposed to run on every request? If so, why not just add this code to the ASCX:
window.onload = doSomething();

Categories