ssm*_*net 140 javascript c# asp.net
有人能提供从CodeBehind调用JavaScript函数的好例子,反之亦然吗?
mut*_*nic 191
你可以试试这个:
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);
Run Code Online (Sandbox Code Playgroud)
The*_*iot 52
C#到JavaScript:您可以注册脚本块以在页面上运行,如下所示:
ClientScript.RegisterStartupScript(GetType(),"hwa","alert('Hello World');",true);
Run Code Online (Sandbox Code Playgroud)
alert()用您的函数名称替换part.
要从JavaScript调用C#方法,您可以使用ScriptManager或jQuery.我个人用jQuery.您需要使用WebMethod属性来装饰要从JavaScript调用的方法.有关PageMethod从jQuery您调用C#方法(被调用)的更多信息,请参阅Dave Ward的帖子.
Orl*_*era 50
从后面的代码调用JavaScript函数
第1步添加您的Javascript代码
<script type="text/javascript" language="javascript">
function Func() {
alert("hello!")
}
</script>
Run Code Online (Sandbox Code Playgroud)
步骤2加1 脚本管理的WebForm中,添加1个按钮太多
步骤3在按钮单击事件中添加此代码
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func()", true);
Run Code Online (Sandbox Code Playgroud)
小智 16
你不能直接这样做.在标准WebForms中,JavaScript由浏览器解释,C#由服务器解释.使用JavaScript从服务器调用方法可以做的是.
WebMethod如attribute在目标的方法.ScriptManager设置EnablePageMethods为true.PageMethods.像这样:
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
}
Run Code Online (Sandbox Code Playgroud)
ScriptManager上Page<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Run Code Online (Sandbox Code Playgroud)
function GetProductsByCategoryID(categoryID)
{
PageMethods.GetProducts(categoryID, OnGetProductsComplete);
}
Run Code Online (Sandbox Code Playgroud)
要从服务器调用JavaScript函数,您可以使用RegisterStartupScript:
ClientScript.RegisterStartupScript(GetType(),"id","callMyJSFunction()",true);
Run Code Online (Sandbox Code Playgroud)
Lik*_*iko 14
如果需要将值作为参数发送.
string jsFunc = "myFunc(" + MyBackValue + ")";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);
Run Code Online (Sandbox Code Playgroud)
你不能.当JavaScript在客户端上运行时,Codebehind正在服务器上运行.
但是,您可以添加<script type="text/javascript">someFunction();</script>到输出中,从而在浏览器解析标记时调用JS函数.
小智 7
你可以做的另一件事是创建一个在后面的代码中设置的会话变量,然后检查该变量的状态,然后运行你的javascript.好处是,这将允许您在您想要的位置运行脚本,而不必弄清楚您是希望它在DOM中运行还是全局运行.
像这样:代码背后:
Session["newuser"] = "false"
Run Code Online (Sandbox Code Playgroud)
在javascript中
var newuser = '<%=Session["newuser"]%>';
if (newuser == "yes")
startTutorial();
Run Code Online (Sandbox Code Playgroud)
你可以使用文字:
this.Controls.Add(new LiteralControl("<script type='text/javascript'>myFunction();</script>"));
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中试试这个,它会 100% 工作
在你的代码隐藏文件中写下这行代码
string script = "window.onload = function() { YourJavaScriptFunctionName(); };";
ClientScript.RegisterStartupScript(this.GetType(), "YourJavaScriptFunctionName", script, true);
Run Code Online (Sandbox Code Playgroud)
这是网页表单页面
<script type="text/javascript">
function YourJavaScriptFunctionName() {
alert("Test!")
}
</script>
Run Code Online (Sandbox Code Playgroud)
IIRC Code Behind是编译服务器端,javascript被解释为客户端.这意味着两者之间没有直接联系.
另一方面,你可以做的是让客户端和服务器通过一个名为AJAX的漂亮工具进行通信.http://en.wikipedia.org/wiki/Asynchronous_JavaScript_and_XML
小智 5
ScriptManager.RegisterStartupScript(this, this.Page.GetType(),"updatePanel1Script", "javascript:ConfirmExecute()",true/>
Run Code Online (Sandbox Code Playgroud)
工作示例:_
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
背后的代码
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
可能的陷阱:-
CodeBehind="History.aspx.cs" 指向错误的页面| 归档时间: |
|
| 查看次数: |
656081 次 |
| 最近记录: |