最佳实践-从Code-Behind设置jQuery属性 [英] Best Practices - set jQuery property from Code-Behind

查看:63
本文介绍了最佳实践-从Code-Behind设置jQuery属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用在后台代码中计算出的值在jQuery命令中设置单个属性.我最初的想法是只使用<%= %>这样访问它:

I need to set a single property in a jQuery command using a value that is calculated in the code-behind. My initial thought was to just use <%= %> to access it like this:

.aspx

<script type="text/javascript" language="javascript">
$('.sparklines').sparkline('html', {
    fillColor: 'transparent',
    normalRangeMin: '0',
    normalRangeMax: <%= NormalRangeMax() %>
});
</script>

.aspx.cs

protected string NormalRangeMax() {
    // Calculate the value.
}

虽然从ASPX页面调用只是获得一个值,这很奇怪.更不用说我有一个完整的方法,该方法只需填充一个属性即可进行少量计算.

It smells odd to have to call from the ASPX page to just get a single value though. Not to mention I have an entire method that does a small calculation just to populate a single property.

一种替代方法是使用clientScriptManager.RegisterClientScriptBlock在代码隐藏中创建整个<script>块.但是我真的不喜欢将JavaScript的全部代码都放在JavaScript后面,因为它本身就是JavaScript.

One alternative would be to create the entire <script> block in the code-behind using clientScriptManager.RegisterClientScriptBlock. But I really don't like putting entire chunks of JavaScript in the code-behind since its, well, JavaScript.

也许如果我最终拥有许多这样的方法,那么我可以将它们放在局部类中,这样至少它们在物理上与其余代码是分开的.

Maybe if I end up having many of these methods I can just put then in a partial class so at least they are physically separate from the rest of the code.

您推荐哪种方法易于理解和维护?

What method would you recommend as being easy to understand and easy to maintain?

推荐答案

<%%>正常运行.我要做的一件事是在页面上的隐藏字段中设置一个值(然后编写必要的javascript以提取该值),这很好,因为我可以通过javascript更改该隐藏字段,并且何时/如果页面回发我可以也可以从后面的代码中获得新的价值.

The <% %> works fine. One thing that I do is set a value in a hidden field on the page (then writing the necessary javascript to extract that value), this is nice because I can change that hidden field via javascript and when/if the page posts back I can get that new value from code behind as well.

如果需要按需调用该方法,则可以对ASP.NET WebMethod进行jQuery AJAX调用以获取数据并重新填充各种选项.您可以在此处找到有关如何执行此操作的很好的教程:

If you need to call the method on demand, you could do an jQuery AJAX call to a ASP.NET WebMethod to grab the data and re-populate the various options. You can find a good tutorial on how to do that here: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

下面是一些使用隐藏字段方法的示例代码(使用datepicker控件,但您会明白的):

Below is some sample code using the hidden field method (using the datepicker control, but you'll get the idea):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtCalendar" runat="server" />
            <asp:HiddenField ID="hfTest" runat="server" />
        </div>
    </form>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

    <script type="text/javascript" src="http://ui.jquery.com/latest/ui/ui.datepicker.js"></script>

    <script type="text/javascript">
    var dateMinimum = new Date($("#<%= hfTest.ClientID %>").val());

    $(function() {
        $("#<%= txtCalendar.ClientID %>")
            .datepicker({
                minDate: dateMinimum
            });
    });
    </script>
</body>

以及Page_Load方法背后的代码:

And the code behind Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
    // Set Value of Hidden Field to the first day of the current month.
    this.hfTest.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).ToString("MM/dd/yyyy");
}

这篇关于最佳实践-从Code-Behind设置jQuery属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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