简体   繁体   English

从C#分配一个JavaScript变量

[英]assign a javascript variable from c#

I have some javascrtipt eg: 我有一些javascrtipt例如:

<script type="text/javascript"> 
flashvars.myval  = "blah"; //get this from c# ..etc 
</script>

i need to assign the variable from c# 我需要从C#中分配变量

how can i do that? 我怎样才能做到这一点? can i call ac# method? 我可以调用ac#方法吗?

im using regular asp.net. 我使用常规的asp.net。

the javascript is on the aspx page. javascript在aspx页面上。

You need to provide more info. 您需要提供更多信息。 Assuming that's on a page, some options are: 假设在页面上,一些选项是:

  1. trigger a postback and have the server send a script with the response to execute after everything is loaded (using RegisterStartupScript) 触发回发,并让服务器发送包含响应的脚本以在加载所有内容后执行(使用RegisterStartupScript)
  2. do an ajax request to get the data from the server, and set the variable or do anything on the callback 进行ajax请求以从服务器获取数据,并设置变量或对回调执行任何操作
  3. if its always sent from the server on web forms do something like (or call a property on the page): 如果它总是从服务器通过Web表单发送的,则执行以下操作(或在页面上调用属性):

    var somevar = '<%=SomePageMethodThatReturnsTheValue()%>'; var somevar ='<%= SomePageMethodThatReturnsTheValue()%>';

  4. if its always sent from the server on asp.net mvc do something like (or ViewData["SomeValue"] if its an untyped view): 如果它总是从asp.net mvc上的服务器发送,则执行以下操作(如果它是无类型的视图,则执行以下操作:)

    var somevar = '<%=Model.MyProperty%>'; var somevar ='<%= Model.MyProperty%>';


After the edit: what u want its option 3. Something like: 编辑后:您想要的选项3.什么类似:

<script type="text/javascript"> 
flashvars.myval  = '<%= SomeMethodInThePageThatReturnsTheValue() %>'; //get this from c# ..etc 
</script>

the code can also be a property that you set on load: <%= SomeProperty %>. 该代码也可以是您在加载时设置的属性:<%= SomeProperty%>。

您可能想看看Script#之类的东西

Is this script a separate file or is it on the aspx page? 该脚本是一个单独的文件,还是在aspx页面上?

If so something like this could work: 如果是这样,这样的事情可能会起作用:

.get({ 'userID': '<%=UserID%>', rest of JSON});

UserID is a C# property. UserID是C#属性。

It depends what technology you are using. 这取决于您使用的技术。

If you are using ASP MVC, pass the data into the view (whatever way suits you) and then use the <%= syntax 如果您使用的是ASP MVC,请将数据传递到视图中(无论哪种方式),然后使用<%=语法

ie

flashvars.myval = <%=ViewData["MyVal"]%>

The answer I'm going to give is going to make a lot of assumptions, but given the lack of detail in your post, they seem fair. 我要给出的答案将做出很多假设,但鉴于您的帖子中缺少细节,因此它们似乎很合理。 First of all, I assume you are running this code in a web page, and secondly I'm going to assume that you have access to code running in a web server. 首先,假设您正在网页中运行此代码,其次,我假设您有权访问在网络服务器中运行的代码。 If both assumptions are correct, you could use AJAX to call code on the server, and populate this value with the output. 如果两个假设都正确,则可以使用AJAX调用服务器上的代码,并使用输出填充该值。

If you are using jQuery, you might want to look here for more information on the javascript code, and here for details on the C# side. 如果您使用的是jQuery,则可能需要在此处查看有关javascript代码的更多信息,并在此处查看C#方面的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM