Digging Into Data Binding Expressions

本文深入探讨ASP.NET数据绑定表达式,介绍其常见场景,如在Repeater和DataGrid中的应用。还展示了可绑定自定义对象集合,以及利用数据绑定改变控件外观。分析了运行时数据绑定原理,对比了Eval和强制类型转换的优劣,并介绍从数据绑定表达式调用代码隐藏类方法的技巧。
 

Data binding expressions in ASP.NET are the small snippets of code you see between <%# and %> characters in an ASPX file. We normally see these expressions in a Repeater’s ItemTemplate declarations, and in a DataGrid's  TemplateColumn markup. Also, Data binding expressions often contain a call to the DataBinder.Eval method. Although these are the common scenarios for data binding in ASP.NET, there is more you can do with a little knowledge of what happens underneath the covers. In this article we will take a look at how the data binding expressions work, where they work, and demonstrate some additional tricks you can use to get more from your data binding expressions.

As a refresher, let’s look at a simple webform with data binding expressions:

<form id="Form1" method="post" runat="server">
  <table>         
    <asp:Repeater id="Repeater1" runat="server">
      <ItemTemplate>
        <tr>
          <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
          <td><%# DataBinder.Eval(Container.DataItem, "HexValue") %></td>
        </tr>
      </ItemTemplate>
    </asp:Repeater>
  </table>
</form>

This produces the web form shown below.

In this example we have a Repeater control displaying color names and their hexadecimal equivalents. We know we can bind a Repeater to DataTable objects, DataView objects, SqlDataReader objects, and others. One of the nice features of data binding is how ASP.NET abstracts away the ultimate source of the data and we don’t need to know the exact type of the object we are binding against.

Our first data binding tip is that you can also bind against a collection of custom objects. For instance, in our sample web form we are using an ArrayList of Color classes, where the Color class is our own custom class, defined below.

public class Color
{
   public Color(string name, byte r, byte g, byte b)
   {
      this.name = name;
      hexValue = String.Format(
                        "#{0:X2}{1:X2}{2:X2}",
                        r, g, b
                     );
   }

   public string Name
   {
      get { return name; }
   }

   public string HexValue
   {
      get { return hexValue; }
   }

   private string name;
   private string hexValue;
}   

The Color class constructor takes a name, and the red, green, and blue values to describe the color. We can build an ArrayList of the class using the following code.

public static ArrayList GetColors()
{
   ArrayList list = new ArrayList();
   
   list.Add(new Color(System.Drawing.Color.AliceBlue.Name, 
                      System.Drawing.Color.AliceBlue.R,
                      System.Drawing.Color.AliceBlue.G,
                      System.Drawing.Color.AliceBlue.B)
            );

   list.Add(new Color(System.Drawing.Color.Beige.Name,
                      System.Drawing.Color.Beige.R,
                      System.Drawing.Color.Beige.G,
                      System.Drawing.Color.Beige.B)
            );

   list.Add(new Color(System.Drawing.Color.Chocolate.Name, 
                      System.Drawing.Color.Chocolate.R,
                      System.Drawing.Color.Chocolate.G,
                      System.Drawing.Color.Chocolate.B)
      );

   list.Add(new Color(System.Drawing.Color.DarkMagenta.Name,
                      System.Drawing.Color.DarkMagenta.R,
                      System.Drawing.Color.DarkMagenta.G,
                      System.Drawing.Color.DarkMagenta.B)
      );

   list.Add(new Color(System.Drawing.Color.Fuchsia.Name, 
                      System.Drawing.Color.Fuchsia.R,
                      System.Drawing.Color.Fuchsia.G,
                      System.Drawing.Color.Fuchsia.B)
      );

   list.Add(new Color(System.Drawing.Color.PapayaWhip.Name,
                      System.Drawing.Color.PapayaWhip.R,
                      System.Drawing.Color.PapayaWhip.G,
                      System.Drawing.Color.PapayaWhip.B)
      );

   list.Add(new Color(System.Drawing.Color.Violet.Name, 
                      System.Drawing.Color.Violet.R,
                      System.Drawing.Color.Violet.G,
                      System.Drawing.Color.Violet.B
                     )
      );

   list.Add(new Color(System.Drawing.Color.Black.Name, 
                      System.Drawing.Color.Black.R,
                      System.Drawing.Color.Black.G,
                      System.Drawing.Color.Black.B
                     )
        );
   return list;
}

Displaying values inside of <td> tags is not the only place to use a data binding expression. You can also use data binding to change the appearance of a control. In the next sample we will set the background color of a row using data binding.

<asp:Repeater id="Repeater1" runat="server">
  <ItemTemplate>
    <tr bgcolor="<%# DataBinder.Eval(Container.DataItem, "HexValue")%>">
      <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
      <td><%# DataBinder.Eval(Container.DataItem, "HexValue") %></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

This gives us the following form.

In the next section we will dig into see how data binding happens at runtime.

Under The Covers Of The Data Binding Expression

In order to really understand what happens inside of the data binding expression, let’s take a look at the code the runtime generates for the ASPX file.

public void __DataBind__control3(object sender, System.EventArgs e) {
   System.Web.UI.WebControls.RepeaterItem Container;
   System.Web.UI.DataBoundLiteralControl target;
   target = ((System.Web.UI.DataBoundLiteralControl)(sender));
   Container = ((System.Web.UI.WebControls.RepeaterItem)(target.BindingContainer));
       
   #line 17 "E:/dev/xprmnt/aspnet/DataBinding/SimpleData.aspx"
   target.SetDataBoundString(0, 
       System.Convert.ToString(DataBinder.Eval(Container.DataItem, "HexValue")));
            
   #line 18 "E:/dev/xprmnt/aspnet/DataBinding/SimpleData.aspx"
   target.SetDataBoundString(1, 
       System.Convert.ToString(DataBinder.Eval(Container.DataItem, "Name")));
             
   #line 19 "E:/dev/xprmnt/aspnet/DataBinding/SimpleData.aspx"
   target.SetDataBoundString(2, 
       System.Convert.ToString(DataBinder.Eval(Container.DataItem, "HexValue")));
       
}

The above is an excerpt from the code generated for our web form into the temporary ASP.NET files directory. It shows us how the Container variable that we use in the call to Eval is set to reference a BindingContainer setup by the runtime. This method, an event handler, will fire for each item the control needs to bind (once for each row, or once for each list item in this example).

The most important point to take from the above code is how the expression we use for data binding is placed as a parameter to Convert.ToString. This means we can use any expression that will yield a string. For example, the following ItemTemplate would produce the same screen we saw in the last screenshot.

<asp:Repeater id="Repeater1" runat="server">
  <ItemTemplate>
    <tr bgcolor="<%# ((Color)Container.DataItem).HexValue %>">	
      <td><%# ((Color)Container.DataItem).Name %></td>
      <td><%# ((Color)Container.DataItem).HexValue %></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

In this case we are skipping a call to DataBind.Eval and casting the DataItem to our Color type (note, in order to do this, you’ll need to import the namespace used where Color is declared with the <@ Import > directive, i.e. <%@ Import Namespace="aspnet.DataBinding" %> if the Color type is in a class file under a namespace of aspnet.DataBinding.

Using the DataBinder.Eval technique allows us to avoid putting messy casts into the ASPX. Instead of casts, Eval uses reflection techniques to dynamically find a property by name at runtime. Because reflection is inherently slow, Eval is relatively slower than using a cast. On the other hand, one advantage to DataBinder.Eval is that the syntax will work in an ASPX compiled for either C# or VB.NET. In the form shown above the casting syntax will only work if the ASPX page compiles with the C# compiler.

While the above example demonstrates how we do not necessarily need to use DataBinder.Eval in our data binding expressions, let’s take the concept one step further and call a method in our code-behind class from the data binding expression.

<asp:Repeater id="Repeater1" runat="server">
  <ItemTemplate>
    <tr bgcolor="<%# ((Color)Container.DataItem).HexValue %>">
      <td><%# GetColorName(Container.DataItem)  %></td>
      <td><%# ((Color)Container.DataItem).HexValue %></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

In the above template we call the GetColorName method and pass the DataItem as a parameter. Calling a method inside of a data binding expression allows us to use additional logic in the compiled, intellisensed environment of our code behind class. The method needs to be declared as a protected method of the code-behind class in order for this to work, because the class dynamically generated from the ASPX markup is derived from the class defined in the code behind file. The method in our code behind is shown below.

protected string GetColorName(object o)
{
   string name = string.Empty;

   Color color = o as Color;
   if(color != null)
   {
      name = color.Name;
      int i = 1;
      do
      {
         if(char.IsUpper(name, i))
         {
            name = name.Insert(i, " ");
            i = i +2;
         }
         else
         {
            i = i + 1;
         }

      } while(i < name.Length);
   }

   return name;
}

The method above will take the name of the color “PapayaWhip” and insert spaces in front of all uppercase letters after the first letter, yielding “Papaya Whip”.

Hopefully this article has demonstrated some additional techniques you can use in data binding scenarios. Don’t limit your self to DataBinder.Eval if the scenario calls for some additional formatting logic!

训练数据保存为deep_convnet_params.pkl,UI使用wxPython编写。卷积神经网络(CNN)是一种专门针对图像、视频等结构化数据设计的深度学习模型,在计算机视觉、语音识别、自然语言处理等多个领域有广泛应用。其核心设计理念源于对生物视觉系统的模拟,主要特点包括局部感知、权重共享、多层级抽象以及空间不变性。 **1. 局部感知与卷积操作** 卷积层是CNN的基本构建块,使用一组可学习的滤波器对输入图像进行扫描。每个滤波器在图像上滑动,以局部区域内的像素值与滤波器权重进行逐元素乘法后求和,生成输出值。这一过程能够捕获图像中的边缘、纹理等局部特征。 **2. 权重共享** 同一滤波器在整个输入图像上保持相同的权重。这显著减少了模型参数数量,增强了泛化能力,并体现了对图像平移不变性的内在假设。 **3. 池化操作** 池化层通常紧随卷积层之后,用于降低数据维度并引入空间不变性。常见方法有最大池化和平均池化,它们可以减少模型对微小位置变化的敏感度,同时保留重要特征。 **4. 多层级抽象** CNN通常包含多个卷积和池化层堆叠在一起。随着网络深度增加,每一层逐渐提取更复杂、更抽象的特征,从底层识别边缘、角点,到高层识别整个对象或场景,使得CNN能够从原始像素数据中自动学习到丰富的表示。 **5. 激活函数与正则化** CNN中使用非线性激活函数来引入非线性表达能力。为防止过拟合,常采用正则化技术,如L2正则化和Dropout,以增强模型的泛化性能。 **6. 应用场景** CNN在诸多领域展现出强大应用价值,包括图像分类、目标检测、语义分割、人脸识别、图像生成、医学影像分析以及自然语言处理等任务。 **7. 发展与演变** CNN的概念起源于20世纪80年代,其影响力在硬件加速和大规模数据集出现后真正显现。经典模型如LeNet-5用于手写数字识别,而AlexNet、VGG、GoogLeNet、ResNet等现代架构推动了CNN技术的快速发展。如今,CNN已成为深度学习图像处理领域的基石,并持续创新。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
内容概要:本文介绍了一种基于CEEMDAN-BiLSTM的中短期天气预测模型,通过将完全集合经验模态分解自适应噪声(CEEMDAN)与双向长短期记忆网络(BiLSTM)相结合,实现对复杂气象时间序列的高精度预测。首先利用CEEMDAN对原始气象数据进行多尺度分解,获得多个本征模态函数(IMF)分量和残差,有效解决模式混叠与噪声干扰问题;随后对各IMF分量分别构建BiLSTM模型进行独立预测,充分发挥其对前后时序依赖的建模能力;最后通过集成重构输出最终预测结果。文中还包含了数据预处理、特征提取、模型评估与可视化等完整流程,并提供了MATLAB实现的部分代码示例。该方法显著提升了天气预测的准确性与鲁棒性,适用于多类气象要素的中短期趋势预测。; 适合人群:具备一定机器学习和时间序列分析基础,从事气象、环境、能源等领域研究或工程应用的研发人员、高校研究生及数据科学家。; 使用场景及目标:①应用于温度、风速、降水等气象变量的中短期精准预测;②解决传统模型在非线性、多尺度气象信号建模中的局限性;③构建智能气象预测系统,服务于电力调度、灾害预警、智慧农业等实际业务场景。; 阅读建议:建议结合MATLAB代码实践操作,深入理解CEEMDAN分解机制与BiLSTM建模细节,重点关注数据预处理、模型参数调优与结果集成策略,同时可扩展至多变量联合预测以提升应用价值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值