简体   繁体   English

如何从C#代码中传输序列化的JSON字符串以在Javascript中使用

[英]How to transfer a serialized JSON string from C# code behind to use in Javascript

The ultimate goal here is to populate fields of a Bootstrap modal from an object in the C# code behind that has all the data I need. 这里的最终目标是从C#代码后面的对象填充Bootstrap模态的字段,该对象包含我需要的所有数据。 I serialized the object from the code behind, like this: 我从后面的代码序列化了对象,如下所示:

JavaScriptSerializer serializer = new JavaScriptSerializer();
sJSON = serializer.Serialize(aerationsystem.AerationSystem);

Now I want to reference that JSON in my Javascript function, like this: 现在,我想在我的Javascript函数中引用该JSON,如下所示:

</form>
    <script type="text/javascript">
        function fillModal() {
            var diameterValue = document.getElementById('diameter');            
            var aeration = <%# sJSON %>;
            diameterValue.innerText = aeration.dBinDiameter;
        }
    </script>
</asp:Content>

(I included that closing form tag and the closing asp:Content tag so you all could see where it was that I put this Javascript: at the very end of the file.) (我包括了结束表单标记和结束asp:Content标记,因此大家都可以看到我在文件的最后放置了Javascript的位置。)

However, when I call that Javascript function, this is what I see in the browser's debugger: 但是,当我调用该Javascript函数时,这是在浏览器的调试器中看到的内容:

    <script type="text/javascript">
        function fillModal() {
            var diameterValue = document.getElementById('diameter');            
            var aeration = ;
            diameterValue.innerText = aeration.dBinDiameter;
        }
    </script>

I got the idea from here: Any way to pass an object from c# code behind to javascript? 我从这里得到的想法是: 任何将c#代码后面的对象传递给javascript的方法? But, the means of accessing that JSON variable does not work for me. 但是,访问该JSON变量的方法对我不起作用。 I've tried moving my script, but when I do that, I get an error that says "The Controls collection cannot be modified because the control contains code blocks (ie <% ... %>).". 我曾尝试移动脚本,但是这样做时,出现一个错误,提示“无法修改控件集合,因为控件包含代码块(即<%...%>)。” And I've tried other delimiters, like <%= %>, but I get the same errors depending on script placement. 而且我尝试了其他定界符,例如<%=%>,但是根据脚本的放置位置,我也会遇到相同的错误。 What am I doing wrong? 我究竟做错了什么? How can I get that JSON string to show up in my Javascript function? 如何获取该JSON字符串以显示在Javascript函数中?

<%# %> is a binding block so you need call the Page.DataBind() method, whether in the Page_Load or the Page_PreRender event. <%# %>是一个绑定块,因此无论在Page_Load还是Page_PreRender事件中,都需要调用Page.DataBind()方法。

You also need to curate the string so the JSON text doesn't break your javascript: 您还需要整理字符串,以便JSON文本不会破坏您的JavaScript:

var aeration = '<%# sJSON.Replace("'", "\'").Replace("\n", " ") %>';

<%# %> syntax is for binding data from data source. <%# %>语法用于绑定来自数据源的数据。

The easiest way is to use a Literal Server Control. 最简单的方法是使用文字服务器控件。 It seems a bit weird inside script tag, but it does the job. 脚本标记内部似乎有点怪异,但却可以完成任务。

<script type="text/javascript">
    function fillModal() {
        var diameterValue = document.getElementById('diameter');
        var aeration = <asp:Literal id="JsonLiteral" runat="server"/>;
        diameterValue.innerText = aeration.dBinDiameter;
    }
</script>

Code Behind 背后的代码

JavaScriptSerializer serializer = new JavaScriptSerializer();
JsonLiteral.Text = serializer.Serialize(aerationsystem.AerationSystem);

thank you so much for taking the time to answer my question! 非常感谢您抽出宝贵的时间回答我的问题! I ended up solving the problem, with some inspiration from you all. 我终于从大家那里得到一些启发,终于解决了这个问题。 Instead of using a Literal control, I ended up using a <asp:HiddenField> control on the page to store the value. 我最终没有使用Literal控件,而是在页面上使用了<asp:HiddenField>控件来存储值。 Then, I pulled that its value into the Javascript variable. 然后,我将其值提取到Javascript变量中。

The hidden field: <asp:HiddenField ID="JsonLiteral" runat="server" /> 隐藏的字段: <asp:HiddenField ID="JsonLiteral" runat="server" />

The code in the event that assigned the value: 分配值的事件中的代码:

JavaScriptSerializer serializer = new JavaScriptSerializer();
JsonLiteral.Value = serializer.Serialize(aerationsystem.AerationSystem);

And the JavaScript that finished the job: 完成工作的JavaScript:

function fillModal() 
    {
        $(document).ready(function () {
            var diameter = document.getElementById('diameter');
            var control = document.getElementById('<%= JsonLiteral.ClientID%>');
            var aeration = JSON.parse(control.value);

            diameter.innerText = aeration.dBinDiameter;
        });
    }

(There's gonna be a lot more to it than just that one property, of course.) (当然,不仅仅可以拥有一个物业,而且还有很多其他功能。)

Again, thank you all for you help, because I could not have done it without you! 再次感谢大家的帮助,因为没有你我做不到!

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

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