Need to call a Javascript method from the code-behind - javascript

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.

Related

Where should I place the ScriptManager.RegisterStartupScript to call my javascript

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);
}

LinkButton click doesn't trigger with no javascript

Web forms user control generating a LinkButton in PageInit:
LinkButton b1 = new LinkButton();
b1.Click += new EventHandler(Button_Click);
public void Button_Click(object sender, EventArgs e) {
//redirects to another page .. (no js defined here or in the ascx surrounding this button)
}
Why doesn't this wok when my javascript is disabled, if I used no js at all when creating the button?
How do I prevent this?
Yes this is webforms (sorry, unable to comment below).
The issue is that's how web forms manages its events with the server - as I understand it anyway (correct me if I'm wrong). Use html 5 instead. You can make the page postback to the server at the same url with a query string and then on the server grab the query string. Not the best solution but it works; it will look something similar to this:
In your page:
<form id="aForm">
<button id="abutton" type"submit" formaction="thisPage.aspx?redirect=true">click me</button>
</form>
In your page load event on the server:
if (IsPostBack) {
object something = Request.QueryString["redirect"];
if (something == null)
//not redirecting
}

Run javascript on the first page load

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.

jQuery call error on Page_Load

I have the following code to register a javascript function on Page_Load (also tried it on Page_Init). The javascript switches two panels from hidden to shown based on a parameter on load of page:
protected void Page_Load(object sender, EventArgs e)
{
String switchAction = "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>";
Page.RegisterClientScriptBlock("switchaction", switchAction);
}
But when the page loads I am receiving an error: $ is not defined.
I looked in Firebug and the jQuery files are being loaded however, the first file that is being loaded in the .Net tab is the page itself. I know the jquery is correct as the same code works on a different page. Where should my RegisterClientScriptBlock be put in the page lifecycle to work correctly when the page loads? Or am I going about this all wrong?
You just need to ensure that the inserted script gets inserted after the JQuery reference.
Use RegisterStartupScript instead -- that inserts the script tag before </form> closing tag.
Not sure if this is relevant but I always use:
<script type="text/javascript"...
Sorry this should have been a comment rather than an answer.
I think it has to do with the register function your using. Try using RegisterStartupScript instead of RegisterClientScriptBlock.
Page.ClientScript.RegisterStartupScript(this.GetType(), "switchaction", "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>", false);

RegisterClientScriptBlock ASP.net

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.

Categories

Resources