I am trying to use ScriptManager.RegisterStartupScript but I am not sure where to place it. For example: on a page load, or inside a DeleteButtonClick even handler method. On my page there's a delete asp.button. And I want to call my JavaScript function every time when a user clicks on the delete button. Thanks
Register the script during the PreRender phase:
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Key",
"alert('call in pre-render');", true);
}
Related
I have my code-behind class and there I have an EventListener, I need to refresh the whole page when my EventListener catches an ´Event´.
How do I do this? I have a JavaScript function in my client-side window.location.reload(true).
The problem is that the javascript never gets executed.
Code-Behind:
private void WebResponse_Msg(object sender, EventArgs e){
ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), "refresh();", true);
}
JS:
<script type="text/javascript">
function refresh() {
window.location.reload(true);
}
</script>
Server side code can not trigger javascript (client side code) whenever you want (like when an event is triggered).
However there are several workarounds that I mention 2 of them:
1.Using SignalR
2.Logging that event when triggered in a Session, and checking the session value by ajax periodically.
I only want to run a javascript method on the page load the 1st time only. If there is a postback I do not want to run that method again.
This method is setting the defaults to certain textboxes.
If I add an onLoad to the body element it runs it each time on the postback. I've tried a ClientScript.RegisterStartupScript but that too runs the method on the postback as well.
Am I going to have to do it old school where I have to increment a hidden value and check that each time?
Try defining the method within your Page_Load event using the IsPostBack property to determine if it is the initial load or not :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// It is the initial load, call your script
ClientScript.RegisterStartupScript(...);
}
}
The Page_Load event will be called every time that your page is loaded, regardless of a PostBack or not. It will only ever be false on the initial load, so if you place your call within there, it should only execute the script a single time.
I am trying to invoke the js function implemented on the aspx page from a .cs class. But, ScriptManager doesn't seem to exist in .cs class. Basically, the .cs file is a part of dll that I am using in my project. I need to invoke the js function that is implemented on the aspx page, from my .cs file present in the dll.
The js function is successfuly invoked from the aspx page, but when I try the same code in .cs file it says
ScriptManager is inaccessible due to its protection level.
here is code I am trying
protected void MyMethod()
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true);
}
any ideas why the same code runs successfuly on the aspx page but not from a .cs class?
ScriptManager.RegisterStartupScript accept Page or Control as first argument. Make sure you pass your current Page to the cs method
protected void MyMethod(Page page)
{
ScriptManager.RegisterStartupScript(page, typeof(UpdatePanel), new Guid().ToString() , "jsfunction();", true);
}
And call from aspx.cs page using:
MyMethod(this.Page);
Well, to overcome the above issue I simply tried with this piece of code
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true);
Notice the use of full namespace with ScriptManager.
In my asp page, I take a screenshot of the client's desktop with an applet and ask them to send it to the server with a single click of a LinkButton. I use runApplet() function to call my applet to capture the screen and assign the strings value to a hidden value. (picture is stored as base64 string) Until here, everything works perfect! However, SendLinkButton_Click doesnt seem to be executing!
This is my link button.
<asp:LinkButton ID="SendLinkButton"
OnClientClick="runApplet(); return false;"
OnClick="SendLinkButton_Click"
Visible="false"
CssClass="portal-arrow portal-button"
runat="server">Send</asp:LinkButton>
This is my Javascript function
function runApplet() {
var msg = document.capture.capture();
var hiddenControl = '<%= inpHide.ClientID %>';
document.getElementById(hiddenControl).value = msg;
}
and this is what's inside of SendLinkButton_Click
protected void SendLinkButton_Click(object sender, EventArgs e)
{
Server.Transfer("Preview.aspx", true);
}
when I put the javascript function to a LinkButton's OnClientClick, and execute this "SendLinkButton_Click" with another LinkButton. It works perfect! But I want them to work with just one click!
Please help!
Your client click is returning false so no postback will be made to the server after this point.
Try changing:
OnClientClick="runApplet(); return false;"
To
OnClientClick="runApplet();"
Remove return false from the OnClientClick attribute. If you return false from it, the postback will not execute.
I want to execute alert when user clicks LinkButton1.It is not working?
protected void LinkButton1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "abc", "<script language=javascript>alert(hi)</script>");
}
You mistyped the function name. Change aler to alert. Also, you forgot the delimiters around the string that you are trying to alert.
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "abc", "alert('hi');", true);
use the below
<linkButton runat="server" id="lnk1" OnClientClick="alert('hi');" />
I hope your sample doesn't have a typo, but you are calling "aler" instead of "alert"!
Also, this doesn't define that your button would run this script when the user clicks the link button, it'll be executed when the page gets loaded.
For having such behavior, you need to use the "OnClientClick" control's property, and set there the name of the function - event handler - that would do the alert.
linkButton1.OnClientClick = "myEventHandler";
And define your script in some JavaScript file or by registering a client script block during the pre-render event of your container control or page.