C#页面前台<%%><%#%><%=%>

本文介绍了ASP.NET中常用的三种数据绑定方法:Eval、Bind及直接使用变量的方式,并提供了实例说明如何进行数据转换和时间格式化。

ASP.net前台绑定用的最多,今天小小总结一下。

1:<%#Eval("")%>

2:<%#Bind("")%>

3:<%=变量%>

1:<%#Eval("数据列")%>  主要用于需要进行更改的数据列

如:我需要将<%#Eval("性别")%> =1 ;将1转换成女。

首先在.CS文件中定义一个方法:

 /// <summary>
        /// 转换性别
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public object ChangeSex(object obj)
        {
            if (obj.ToString() == "0")
            {
                return "男";
            }
            else 
            {
                return "女";
            }
        }

<%#ChangeSex(Eval("性别"))%>=女

可以把这些公共的转换方法全部放到一个公共类文件中,进行调用之。调用方法为:

<%#命名空间.公共类名.方法名("列")%>

<%# Common.ChangeSex(Eval("性别"))%>

也可以进行时间截取

如1: <%# Eval("StartTime", "{0:yyyy-MM-dd}")%>

或者2: <%# Convert.ToDateTime(Eval("StartTime").ToString()).ToString("yyyy-MM-dd")%>

2:<%#Bind("数据列")%>

常见于GridView中的不需要进行处理的数据列绑定。

3:<%=变量%>

需要在后台.cs文件中声明一个public的变量

public string userInfo= "数据";

直接调用就OK了

但是我们一般不会这样直接写一个定值;

就需要这样写了:

public string StrOrders
        {
            set
            {
                ViewState["Orders"] = value;
            }
            get
            {
                if (ViewState["Orders"] == null)
                {
                    ViewState["Orders"] = "";
                }
                return ViewState["Orders"].ToString();
            }
        }


这样就OK了。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShengGongSiGuanJianZhiBiaoList.aspx.cs" Inherits="ShengGongSiGuanJianZhiBiaoList" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="../css/css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script> <style type="text/css"> #head { height: auto; width: 900px; margin-left: auto; margin-right: auto; } #table01 { border-left: #99bbe8 solid 1px; border-top: #99bbe8 solid 1px; } #table01 th, #table01 td { border-right: #99bbe8 solid 1px; border-bottom: #99bbe8 solid 1px; width: 2%; font-size: 12px; height: 30px; text-align: center; } #table01 th { background: #f5fafa; color: #047E6C; font-weight: bold; } #no_border { border-left: 0px; border-right: 0px; border-bottom: 0px; } #cccc { border-right: #99bbe8 solid 1px; border-left: #99bbe8 solid 1px; border-bottom: #99bbe8 solid 1px; width: 2%; font-size: 12px; height: 30px; text-align: center; background: #f5fafa; } </style> <script type="text/javascript"> function validate() { $("input[name^='yi_pingfenbiaozhun_']").attr("name", "yi_pingfenbiaozhun_"); $("input[name^='ba_pingfen_']").attr("name", "ba_pingfen_"); return true; } </script> </head> <body> <form id="form1" runat="server"> <center> <div> <br /><br /> <table style="width: 90%; border-top: #99bbe8 solid 1px;" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <th id="cccc" colspan="11" class="auto-style1"><span style="color: black; font-size: x-large"><asp:DropDownList ID="dlyear" runat="server"> <asp:ListItem Value="2025">2025</asp:ListItem> <asp:ListItem Value="2026">2026</asp:ListItem> <asp:ListItem Value="2027">2027</asp:ListItem> <asp:ListItem Value="2028">2028</asp:ListItem> </asp:DropDownList>年<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> <asp:ListItem Value="3">3</asp:ListItem> <asp:ListItem Value="4">4</asp:ListItem> <asp:ListItem Value="5">5</asp:ListItem> <asp:ListItem Value="6">6</asp:ListItem> <asp:ListItem Value="7">7</asp:ListItem> <asp:ListItem Value="8">8</asp:ListItem> <asp:ListItem Value="9">9</asp:ListItem> <asp:ListItem Value="10">10</asp:ListItem> <asp:ListItem Value="11">11</asp:ListItem> <asp:ListItem Value="12">12</asp:ListItem> </asp:DropDownList>省公司关键业绩指标排名</span></th> </tr> </table> <br /> <div> <asp:Button ID="Button2" runat="server" CssClass="Button" Text="查 询" OnClick="Button2_Click"></asp:Button> <asp:Button ID="Button1" runat="server" OnClientClick="return validate()" CssClass="Button" Text="保 存" OnClick="Button1_Click"></asp:Button> </div> <br /> <table id="table01" style="width: 100%" cellpadding="0" cellspacing="0"> <tr> <th>序号</th> <th>考核指标</th> <th>排名</th> <th>一级指标牵头部门</th> <th>一级指标责任部门名称</th> <th>二级指标</th> <th>二级指标责任部门</th> </tr> <%if (dtbm.Rows.Count > 0) { foreach (DataRow bmRow in dtbm.Rows) { string firstId = bmRow["uid"].ToString(); // 1. 先完整计算一级指标总嵌套行数(修复核心:先计算后使用) int firstTotalRows = 1; // 一级自身行 DataRow[] secondRows = dtsan.Select("一级Id='" + firstId + "'"); if (secondRows.Count() > 0) { firstTotalRows = 0; } foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级自身行 + 三级行数 firstTotalRows += secondTotalRows; // 累加所有二级的总行数 } // 2. 获取一级责任部门 DataRow[] firstDeptRows = dtbminfo.Select("一级Id='" + firstId + "'"); string firstDeptName = firstDeptRows.Length > 0 ? firstDeptRows[0]["一级指标责任部门名称"].ToString() : ""; %> <!-- 一级指标行(使用预先计算好的firstTotalRows) --> <tr> <td rowspan="<%= firstTotalRows %>"><%= bmRow["序号"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"><%= bmRow["考核指标"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"> <input type="hidden" id="yi_pingfenbiaozhun_<%=bmRow["uid"].ToString()%>" name="yi_pingfenbiaozhun_<%=bmRow["uid"].ToString()%>" value="<%=bmRow["uid"].ToString() %>" style="width: 98%" /> <input type="text" style="width:20px" value="" name="ba_pingfen_<%= bmRow["uid"].ToString() %>" /> </td> <td rowspan="<%= firstTotalRows %>"><%= bmRow["一级指标牵头部门"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"><%= firstDeptName %></td> <!-- 处理二级指标 --> <%if (secondRows.Length > 0) { bool isFirstSecond = true; foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级合行数 %> <!-- 二级指标行 --> <%if (isFirstSecond) { isFirstSecond = false; %> <td rowspan="<%= secondTotalRows %>"><%= sanRow["二级指标"].ToString() %></td> <%} else { %> <tr> <td rowspan="<%= secondTotalRows %>"><%= sanRow["二级指标"].ToString() %></td> </tr> <%}%> <!-- 处理三级指标(二级责任部门) --> <%if (thirdRows.Length > 0) { foreach (DataRow wuRow in thirdRows) { %> <tr> <td><%= wuRow["二级指标责任部门名称"].ToString() %></td> </tr> <%} } else { %> <tr> <td></td> </tr> <%}%> <%} } else { %> <td></td> <td></td> <%}%> </tr> <% } } else { %> <tr> <td colspan="7" style="height:30px;">暂无数据</td> </tr> <%}%> </table> </div> </center> </form> </body> </html>按照这个代码生成,二级指标和二级责任部门要保留
07-19
ButtonManager = { templates: { devlist: ` <div class="col-sm-12"> <div class="select-list"> <ul> <li> 设备编码:<input type="text" id="querydevCode"/> </li> <li> 设备名称:<input type="text" id="querydevName"/> </li> <li> 固定资产编码:<input type="text" id="queryassetCode"/> </li> <li> 生产厂家:<input type="text" id="queryfactory"/> </li> <li> <a class="btn btn-primary btn-rounded btn-sm" onclick="refreshListTable()"><i class="fa fa-search"></i> 搜索</a> <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i> 重置</a> </li> </ul> </div> </div> <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:devledger:add"> <i class="fa fa-plus"></i> 添加 </a> <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:devledger:remove"> <i class="fa fa-remove"></i> 删除 </a> <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:devledger:export"> <i class="fa fa-download"></i> 导出 </a> `, devinfo: ` `, equit: ` <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:base:add"> <i class="fa fa-plus"></i> 添加 </a> <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:base:edit"> <i class="fa fa-edit"></i> 修改 </a> <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:base:remove"> <i class="fa fa-remove"></i> 删除 </a> <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:base:export"> <i class="fa fa-download"></i> 导出 </a> `}可以添加script样式吗?
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值