• 如何使用DataBinder.Eval()方法进行数据绑定



    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <html>
        
    <script language="C#" runat="server">

        
    void Page_Load(Object semder, EventArgs e) 
        
    {
            
    // 创建数据库连接字符串及SqlDataAdapter对象
            string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"];
            SqlConnection myConnection 
    = new SqlConnection(ConnStr);
            SqlDataAdapter myCommand 
    = new SqlDataAdapter("select * from Titles", myConnection);
            
    // 生成DataSet对象并填充数据
            DataSet ds = new DataSet();
            myCommand.Fill(ds, 
    "Titles");
            
    // 将Repeater控件进行数据绑定
            MyRepeater.DataSource = ds.Tables["Titles"].DefaultView;
            MyRepeater.DataBind();
        }


        
    </script>
        
    <body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
            
    <ASP:Repeater id="MyRepeater" runat="server">
                
    <HeaderTemplate>
                    
    <table width="100%" style="font: 8pt verdana">
                        
    <tr style="background-color:DFA894">
                            
    <th>Title</th>
                            
    <th>Title ID</th>
                            
    <th>Type</th>
                            
    <th>Publisher ID</th>
                            
    <th>Price</th>
                        
    </tr>
                
    </HeaderTemplate>
                
    <ItemTemplate>
                    
    <tr style="background-color:FFECD8">
                        
    <td>
                            
    <%# DataBinder.Eval(Container.DataItem, "title"%>
                        
    </td>
                        
    <td>
                            
    <%# DataBinder.Eval(Container.DataItem, "title_id"%>
                        
    </td>
                        
    <td>
                            
    <%# DataBinder.Eval(Container.DataItem, "type"%>
                        
    </td>
                        
    <td>
                            
    <%# DataBinder.Eval(Container.DataItem, "pub_id"%>
                        
    </td>
                        
    <td>
                            
    <%# DataBinder.Eval(Container.DataItem, "price""$ {0}"%>
                        
    </td>
                    
    </tr>
                
    </ItemTemplate>
                
    <FooterTemplate>
                    
    </table>
                
    </FooterTemplate>
            
    </ASP:Repeater>
        
    </body>
    </html>

    =============================================================

    DataBinder.Eval 方法


    全部显示
    在运行时使用反射来分析和计算对象的数据绑定表达式。此方法允许 RAD 设计器(如 Visual Studio .NET)轻松地生成和分析数据绑定语法。该方法也可通过声明方式在 Web 窗体页上使用,以简化类型之间的转换。

    重载列表
    运行时计算数据绑定表达式。


    [C#] public static object Eval(object, string);

    在运行时计算数据绑定表达式,并将结果格式化为要在请求浏览器中显示的文本。

    [C#] public static string Eval(object, string, string);

    示例
    [Visual Basic, C#, JScript] 下面的示例说明如何以声明方式使用 Eval 方法以绑定到 Price 字段。本示例使用的容器语法假定您正在使用一个列表 Web 服务器控件。格式参数将数字格式化为将由请求浏览器显示的区域设置特定的货币字符串。

    [Visual Basic, C#, JScript] 注意   此示例显示如何使用 Eval 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。


    [C#]
    <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>

  • 相关阅读:
    caffe简单介绍
    良好地去规划自己的学习
    计算机视觉开篇---读史可以明智
    Oracle的闪回操作
    数据库的管理
    oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
    一次导如数据库时主表数据丢失的解决过程
    Oracle函数的使用
    MinGW和MSYS的自动安装 【转】
    Oracle表的管理
  • 原文地址:https://www.cnblogs.com/yeagen/p/1331662.html
Copyright © 2020-2023  润新知