Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 437 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling JavaScript Function From CodeBehind

#11
ScriptManager.RegisterStartupScript(this, this.Page.GetType(),"updatePanel1Script", "javascript:ConfirmExecute()",true/>
Reply

#12
this works for me

object Json_Object=maintainerService.Convert_To_JSON(Jobitem);
ScriptManager.RegisterClientScriptBlock(this,GetType(), "Javascript", "SelectedJobsMaintainer("+Json_Object+"); ",true);
Reply

#13
Another thing you could do is to create a session variable that gets set in the code behind and then check the state of that variable and then run your javascript. The good thing is this will allow you to run your script right where you want to instead of having to figure out if you want it to run in the DOM or globally.

Something like this:
Code behind:

Session["newuser"] = "false"

In javascript

var newuser = '<%=Session["newuser"]%>';
if (newuser == "yes")
startTutorial();
Reply

#14
Working Example :_

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.Master" AutoEventWireup="true" CodeBehind="History.aspx.cs" Inherits="NAMESPACE_Web.History1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script type="text/javascript">

function helloFromCodeBehind() {
alert("hello!")
}


</script>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<div id="container" ></div>

</asp:Content>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NAMESPACE_Web
{
public partial class History1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "helloFromCodeBehind()", true);
}

}
}

Possible pitfalls:-

1. Code and HTML might not be in same namespace
2. `CodeBehind="History.aspx.cs"` is pointing to wrong page
3. JS function is having some error
Reply

#15
I've been noticing a lot of the answers here are using `ScriptManager.RegisterStartupScript` and if you are going to do that, that isn't the right way to do it. The right way is to use `ScriptManager.RegisterScriptBlock([my list of args here])`. The reason being is you should only be using RegisterStartupScript when your page loads (hence the name RegisterStartupScript).

In VB.NET:

ScriptManager.RegisterClientScriptBlock(Page, GetType(String), "myScriptName" + key, $"myFunctionName({someJavascriptObject})", True)

in C#:

ScriptManager.RegisterClientScriptBlock(Page, typeof(string), "myScriptName" + key, $"myFunctionName({someJavascriptObject})", true);

Of course, I hope it goes without saying that you need to replace key with your key identifier and should probably move all of this into a sub/function/method and pass in key and someJavascriptObject (if your javascript method requires that your arg is a javascript object).

MSDN docs:

[To see links please register here]

Reply

#16
I used ScriptManager in Code Behind and it worked fine.

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "CallMyFunction", "confirm()", true);

If you are using UpdatePanel in ASP Frontend.
Then, enter UpdatePanel name and 'function name' defined with script tags.
Reply

#17

ScriptManager.RegisterStartupScript(Page, GetType(), "JavaFunction", "AlertError();", true);


**using your function is enough**
Reply

#18
If you need to send a value as a parameter.

string jsFunc = "myFunc(" + MyBackValue + ")";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);
Reply

#19
You can not do this directly. In standard WebForms JavaScript is interpreted by browser and C# by server. What you can do to call a method from server using JavaScript is.

- Use [`WebMethod`][1] as [`attribute`][2] in target methods.
- Add [`ScriptManager`][3] setting [`EnablePageMethods`][4] as `true`.
- Add JavaScript code to call the methods through the object `PageMethods`.

Like this:

Step 1
------

<!-- language: c# -->

public partial class Products : System.Web.UI.Page
{
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static List<Product> GetProducts(int cateogryID)
{
// Put your logic here to get the Product list
}

Step 2: Adding a [`ScriptManager`][3] on the [`Page`][5]
--------------------------------------------

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Step 3: Calling the method using JavaScript
-----------------------------------------------

<!-- language: lang-js -->

function GetProductsByCategoryID(categoryID)
{
PageMethods.GetProducts(categoryID, OnGetProductsComplete);
}

[Take a look at this link.][6]

To call a JavaScript function from server you can use [`RegisterStartupScript`][7]:

<!-- language: c# -->

ClientScript.RegisterStartupScript(GetType(),"id","callMyJSFunction()",true);


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

[7]:

[To see links please register here]

Reply

#20
Since I couldn't find a solution that was code behind, which includes trying the `ClientScript` and `ScriptManager` like mutanic and Orlando Herrera said in this question (they both somehow failed), I'll offer a front-end solution that utilizes button clicks to others if they're in the same position as me. This worked for me:

HTML Markup:

<asp:button ID="myButton" runat="server" Text="Submit" OnClientClick="return myFunction();"></asp:button>

JavaScript:

function myFunction() {
// Your JavaScript code
return false;
}

I am simply using an ASP.NET button which utilizes the `OnClientClick` property, which fires client-side scripting functions, that being JavaScript. The key things to note here are the uses of the `return` keyword in the function call and in the function itself. I've read docs that don't use `return` but still get the button click to work - somehow it didn't work for me. The `return false;` statement in the function specifies a postback should NOT happen. You could also use that statement in the `OnClientClick` property: `OnClientClick="myFunction() return false;"`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through