English 中文(简体)
拨打来自 ASP. NET C# 代码后方的 Javascript (Raphael.js 库)
原标题:Calling Javascript (Raphael.js library) from C# codebehind in ASP.NET

我试图从 C# 后面的代码中调用一些功能。 我正在寻找如何做到这一点, 并且能够当我想显示它的时候叫出一个警告。 但是, 我不知道如何调用其它的东西!

这就是我需要从后面的代码里 呼唤的:

var paper = Raphael("paper1", 800, 800);
var Plan = paper.path("M100, 100 a 50, 50 0 0,1 50, 50 l -50 0 l 0, -50");
Plan.attr({ fill: "#FF6600" });

我在普通的 HTML 文件上尝试过这些, 但我无法使用它。 我同时也使用主页, 我所找到的大多数例子都没有主页, 因此我对此非常迷茫。

谁能帮上忙?

最佳回答

在.aspx 页面中创建 Javascript 函数, 然后从下面的代码调用此函数 :

在 html 代码中的函数

function dostuff()
{
  // code here
}

C# code in code behind
 protected void callmethod()
    {
        StringBuilder oSB = new StringBuilder();
         Type cstype = this.GetType();


        try
        {
            oSB.Append("<script language=javaScript>");
            oSB.Append("dostuff();");
            oSB.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(cstype, Guid.NewGuid().ToString(), oSB.ToString(), false);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            oSB = null;
        }
    }
问题回答

Javascript 只能在客户端被调用。 如果您绝对需要从服务器上调用, 您可以使用一个 asp: Hidden Field s 值作为您返回时需要 javascript 代码时的旗帜, 然后在符合要求时运行所需的 javascript 。

但这不是一个不错的解决方案,你也许应该尝试 将服务器和客户区分开来。

希望这能帮上忙,无论如何!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签