<script runat=server>,<%%>区别

本文详细解析了ASP.NET中&lt;scriptrunat=server&gt;、&lt;%%&gt;和&lt;%#%&gt;三种代码段的区别与用途。&lt;scriptrunat=server&gt;用于声明成员变量和方法;&lt;%%&gt;用于页面呈现过程中的代码执行;&lt;%#%&gt;则用于数据绑定表达式。
①<script runat="server">代码段与<%%>内联代码段的区别

在asp.net页面的aspx文件中允许使用<script runat="server">和<%%>标记来插入代码片断。这两种方式有什么区别呢?

<script runat="server">标记中的代码跟.cs文件中的代码地位是一致的,比如像下面这样声明一段事件处理代码

<script runat="server">
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Trace.Warn("TextBox1_TextChanged");
    }
</script>

<%%>标记,其实更多的是为了兼容asp的语法而存在的

在asp.net中是用该标记有如下限制:

1.内联代码块必须使用页的默认语言进行编写。

2.不能在<%%>标记中声明函数或者子例程

另外需要注意的是<%%>标记内的内联代码是在页面呈现的Render过程中执行的

已经接近页面生命周期的尾部

总结一下:

<script runat="server"></script>,在服务器端运行;  
<%%>可以将服务器端的代码返回值显示到客户端。

②<script runat=server>、<%%>和<%#%>的区别
<script runat=server>:Code Declaration Blocks
代码声明区
,用于声明此ASPX页面动态编译为CS文件时的成员变量和方法,此区域内不能直接放置语句和表达式,只能放置方法、属性等内容
<%%>:Code Render Blocks
渲染代码区
,放置在此区域内的代码将会被编译到PAGE类的Render方法中,编译顺序按照ASPX的自然顺序。此区域内只能放置语句不能直接放置表达式,也不能放置方法属性等 ;<%=[表达式]%>是<%Response.Write([表达式]);%>的缩写.
<%#%>:Data Binding Expression Syntax
数据绑定表达式
,在其中放置与数据绑定相关的表达式。
<%@ 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
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <style type="text/css"> .GridViewStyle { border-collapse: collapse; width: 100%; text-align:center } .GridViewStyle th, .GridViewStyle td { border: 1px solid #ddd; padding: 8px; } .GridViewStyle tr:hover { background-color: #f1f1f1; } </style> <body> <form id="form1" runat="server"> <div> <asp:Button ID="downloadmon1" runat="server" Text="下载数据" OnClick="downloadmon1_Click" /> <asp:FileUpload ID="FileUp" runat="server" Text="选择文件"/> <asp:Button ID="btn_ExInto_pass" runat="server" Text="导入" OnClick="btn_ExInto_pass_Click" /> </div> <div> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CssClass="GridViewStyle" DataKeyNames="Dept,Week,Month" AutoGenerateEditButton="False" AllowPaging="True" PageSize="20" Visible="false"> <Columns> <asp:BoundField DataField="Plant" HeaderText="Plant" ReadOnly="True" /> <asp:BoundField DataField="Dept" HeaderText="Dept" ReadOnly="True"/> <asp:BoundField DataField="Month" HeaderText="Month" ReadOnly="True"/> <asp:BoundField DataField="Week" HeaderText="Week" ReadOnly="True"/> <asp:TemplateField HeaderText="OT_Time"> <ItemTemplate> <asp:Label ID="lblEmail" runat="server" Text='<%# Bind("OT_Time") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("OT_Time") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </EditItemTemplate> </asp:TemplateField> </Columns> <HeaderStyle Wrap="False" /> </asp:GridView> </div> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="GridViewStyle" DataKeyNames="Dept,Week,Month" DataSourceID="SqlDataSource1" AutoGenerateEditButton="False" AllowPaging="True" PageSize="20"> <Columns> <asp:BoundField DataField="Plant" HeaderText="Plant" ReadOnly="True" /> <asp:BoundField DataField="Dept" HeaderText="Dept" ReadOnly="True"/> <asp:BoundField DataField="Month" HeaderText="Month" ReadOnly="True"/> <asp:BoundField DataField="Week" HeaderText="Week" ReadOnly="True"/> <asp:TemplateField HeaderText="OT_Time"> <ItemTemplate> <asp:Label ID="lblEmail" runat="server" Text='<%# Bind("OT_Time") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("OT_Time") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </EditItemTemplate> </asp:TemplateField> </Columns> <HeaderStyle Wrap="False" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT A.* FROM [Assembly].[dbo].[MPE_HC_OT] AS A LEFT JOIN (SELECT TOP 1 'WW'+Week AS Time FROM [Assembly].[dbo].[MPE_Loading_PTime_Dept_Week] WHERE DEPT='AG1') AS B ON A.WEEK>= B.TIME WHERE A.Week >= B.Time ORDER BY A.Month" UpdateCommand="UPDATE MPE_HC_OT SET OT_Time=@OT_Time,OT_Time_Active='1' WHERE Dept=@Dept and Month=@Month and Week= @Week"> </asp:SqlDataSource> </div> </form> </body> </html>
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值