如何从代码隐藏中分配 $(document).ready 中的值
How to assign value in $(document).ready from code behind
我正在使用Twitter引导程序开发一个.NET应用程序。
我正在尝试将数据从.aspx.cs页面获取到.aspx页面。
请在下面找到我的代码:
斯特奥布莱特.cs
public class strObject
{
public string Name { get; set; }
public string Description { get; set; }
}
.aspx.cs页:
public List<strObject> stringList = new List<strObject>();
protected void Page_Load(object sender, EventArgs e)
{
strObject ObjOne=new strObject();
ObjOne.Name="One";
ObjOne.Description ="One";
stringList.Add(ObjOne);
strObject ObjTwo=new strObject();
ObjTwo.Name="Two";
ObjTwo.Description ="Two";
stringList.Add(ObjTwo);
strObject ObjThree=new strObject();
ObjThree.Name="Three";
ObjThree.Description ="Three";
stringList.Add(ObjThree);
}
。.aspx:
<asp:Panel ID="pnlData" runat="server" style="background-color:White;">
<script type="text/javascript">
$(document).ready(function () {
var valueAssigned=stringList;
});
</script>
</asp:Panel>
我无法在 $(document).ready 中获取 stringList 值。
请帮助我获得价值。
看起来您的stringList
实际上是对象的集合。为了在 JavaScript 中使用它,您需要将其序列化为 JavaScript 对象。
var valueAssigned=<%=new JavaScriptSerializer().Serialize(stringList)%>;
这样您就可以得到以下内容:
var valueAssigned= [{Name: "Foo", Description: "Bar"}, {...}];
编辑
JavaScriptSerializer
在System.Web.Script.Serialization
- 您需要在下面的<%@ Page
顶部添加此内容
<%@ Import Namespace="System.Web.Script.Serialization" %>
或指定 FQ 名称
var valueAssigned=<%=new System.Web.Script.Serialization.JavaScriptSerializer()
.Serialize(stringList)%>;
StuartLC的回答就足够了。JSON是用于此目的的非常好的选择。其他选项可以是在 aspx.cs 中注册客户端脚本。这是另一个关于这个问题的SO问题
如何将数据从 c# 传递到 jquery/javascript?
相关文章:
- jQuery document.ready not working
- jQuery document.ready停止代码
- 停止对document.ready函数的重定向/刷新
- 如果没有document.ready(),我怎么能拥有多个javascript函数呢
- ASPX Jquery 1.11 $(Document).ready(function() {} was skipped
- 为什么不'加载$(document.ready(function)后,单击“工作”
- 从Document.ready中的bundle执行javascript
- 从 document.ready 调用 jQuery 插件抛出 getPreventDefault 错误
- 调用 Document.ready 外部的 AJAX 函数
- jQuery "$(document).ready(function () {" equivalen
- 尝试触发点击document.ready中Bigcommerce Classic Next主题中的产品选项
- document.ready是在async js之后加载的
- 当在document.ready中使用async=true时会发生什么
- 说明ajax何时在document.ready中调用
- 如何在document.ready(function)时运行隐藏函数和动画
- HTML方法未使用Document Ready加载值
- 超越了在document.ready()中编写简单函数
- Javascript on click事件在函数中不起作用,但在document.ready()中起作用
- 为什么document.ready中的事件处理程序函数有效但无效;取出后不起作用
- 在document.ready jquery上运行ajax请求