如何将数据从C#传递到jquery/javascript? [英] How do I pass data from c# to jquery/javascript?

查看:64
本文介绍了如何将数据从C#传递到jquery/javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在页面加载时运行的功能,因此可以在document.ready或pageLoad中使用.

I have a function that I'd like to run when the page loads, so either in document.ready or pageLoad.

使用jquery,我将使用其类名触发该函数

Using jquery, I'll trigger the function using its class name

在document.ready中

In document.ready

var span = $('.linkify');
span.html(textToLinks(span.html()));

其他地方

function textToLinks(text) {

    var exp = /(my text)/ig;
    return text.replace(exp, "<a class='link' href='http://www.bbc.co.uk' target='_blank' >$1</a>");

}

现在这适用于简单的测试数据,但是我现在需要确定如何扩展功能.

Now this works with the simple test data, but I need to now work out how to expand the functionality.

我在c#应用程序中有一个术语列表以及相关的URL.我正在考虑将此数据作为字符串传递并拆分,但是我的JavaScript知识有很多漏洞.

I have a list of terms in my c# app, along with the relevant url. I'm thinking of passing this data as a string, and splitting in, however my javascript knowledge has a lot of holes.

所以我想我有3个问题:

So I suppose I have 3 questions:

  1. 页面加载后如何将字符串放入函数中?
  2. 字符串是正确的类型还是可以传递其他字典对象?
  3. 我如何遍历有效通过的每个术语?

谢谢.

推荐答案

您可以这样:

在后面的代码中将公共成员声明为您的C#类

Declare public member into your C# class on code behind

public string mystring;

在初始化或加载事件中初始化 并将其写在您的js函数上

Initiate it in Init or Load Event And write that on your js function

var exp = '<%=mystring %>';

它应该适用于字符串.对于Dictionnary,您可以尝试使用Ajax

It should work for string. For Dictionnary, you may try to do Ajax

这篇关于如何将数据从C#传递到jquery/javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆