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);
Related
I've have a page which currently does a small task then redirects to a 3rd party website.
The redirect is triggered by a Response.Redirect();
Now I want to add another function but this time it's javascript.
I'm using the following method to add the javascript to the page:
if (!page.ClientScript.IsStartupScriptRegistered(key))
{
ScriptManager.RegisterStartupScript(page, typeof(Page), key, script, false);
}
The function of the javascript is to track a sale, so I need the page to render it and trigger the track before moving on.
Anyone know if this will work or not, and if not how it can be achieved?
You're not going to be able to use Response.Redirect() to handle redirecting the user anymore. Instead, you'll have to redirect through JavaScript after your start-up script executes. I would simply tack on:
window.location.replace("http://www.whateveryourredirecturlis.com");
To the end of your existing start-up script.
Use something like this in your javascript.
document.onready = function () { window.location = "http://yoururl"; };
My situation is following:
I have OnTextChanged event handler on my aspx page, what calls TextChanged() method from code-behind. Now, I need to run a javascript script at the end of that TextChanged() method. How can I do this? Does ScriptManager have any methods what will execute the scripts only then when I change text in my textbox?
I might be a duplicate question, but I couldnt find information whether the scriptmanager has any methods that will run script immediately after registering it.
The actual problem is that, I need to put a script at the end of TextChanged() method and the script will just have to set focus from one html element to another.
You can simply send the required JavaScript in the code behind itself:
public void TextChanged(object sender, EventArgs e)
{
//...
this.ClientScript.RegisterStartupScript(this.Page.GetType(), "text_changed", "alert('Text was changed');", true);
}
This will show the alert only after TextChanged() is executed in the code behind.
If you're not inside a Page instance (e.g. static library) have such code, assuming you're in the context of a Page and not in some HTTP Module or something like that.
ScriptManager.RegisterStartupScript(HttpContext.Current.Handler as Page, typeof(Page), "text_changed", "alert('Text was changed');", true);
write below code in text changed function where you want to execute script. And put your java script function name in place where i bold text
**>ClientScript.RegisterStartupScript(this.GetType(), "Javascript","JavaScriptFunctionName()", true);**
When I load page I load this script:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"question",
"<script type = 'text/javascript'>if(confirm('Are you sure you want to do this?')) return true; else return false;</script>",
false);
I don't know how to handle when user click on yes to do something in code behind and when click on no to do some other thing?
How to handle this click in code behind?
if you are returning true/false, then in the code behind you will only get true.
To get values for false also you will have to create another function.
The best method that i like is setting the value of the return parameter in hiddenfield. And the getting this variable in codebehind and the working accordingly.
You have already have code for that.
You can only call javascript functions on Yes or NO. But Javascript function can call codebehind webmethods using Ajax.
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"question",
"<script type = 'text/javascript'>if(confirm('Are you sure you want to do this?')){[Call Function for Yes]} else{ [Call Function for No or Cancel];}</script>",
false);
Other than the WebMethods technique, you also have an option of using the __doPostBack method to hit code-behind from your javascript code. More info on its usage at http://aspalliance.com/895_Understanding_the_JavaScript___doPostBack_Function.all, http://dopostback.net/.
A limitation of using WebMethods is, since they are always static, your non-static fields, methods, and controls won't be accessible inside them. http://forums.asp.net/t/1531113.aspx/1.
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);
}
Ok, I've got a lightbox with a small form (2 fields) in it, inside an UpdatePanel, and I want to close this lightbox (must be done via javascript) when the 'Save' button is pressed.
However, there is a need to have a server-side CustomValidator on the page, and I only want to close the lightbox if this returns as valid.
Does anyone know a way to trigger javascript (or jQuery) code from a server-side validator?
You can add a little snippet of code using the ScriptManager to execute after the response comes back to the UpdatePanel.
if (Page.IsValid){
ScriptManager.RegisterStartupScript(
customValidator1,
typeof(MyPageClass),
"closeBox",
"myLightBoxVariableOnThePage.close()",
true);
}
When that server side validator runs, it will send a whole new page to the browser. Anything that was shown in the browser before was destroyed, including any state kept in your javascript. If new page bears a strong resemblance to the old page, you should consider this a happy coincidence.
Therefore, the thing to do here is rather than executing a javascript function, have your CustomValidator make the correct changes to the page on success so that it's rendered to the browser correctly in the first place.