由<asp:DropDownList>理解Page.isPostBack的作用

本文探讨了在ASP.NET中使用DropDownList控件时常见的两个问题:一是如何正确获取选中的值,二是如何避免因未检查Page.isPostBack而导致的初始化错误。

由于要使用<asp:DropDownList>实现下拉列表功能,所以在使用时,主要烦了两个错误,其一是在使用<asp:DropDownList>控件时,还是像使用<asp:TextBox>那样,在取值时,还是使用this.ID.Text,所以在取到值的时候,总是默认的第一个值,忽略了<asp:DropDownList>的特性,要使用this.ID.SelectText.Text;其二就是忘了在加上if(!isPostBack),也就是忘了在is(!isPostBack){}中进行对<asp:DropDownList>的DataSource进行初始化,也因此出现了在取值的时候,总是取的是第一个值。

         Page.isPostBack的作用就是如果是第一次浏览网页,就传回false,如果不是第一次浏览网页就传回true。为什么要在if(!isPostBack){}中进行初始化操作(也就是说,所有相关的初始化操作都是在此进行),原因主要是因为页面中存在着点击事件等需要第二次浏览网页(也就是说的是当网页第一次加载,就会调用Page_Load()方法,但是如果网页中含有Button控件等需要进行事件操作,那么此时先要执行还是Page_Load()方法,然后才是相应的事件操作),所以相关初始化操作要在if(!isPostBack)中进行操作。

<%@ 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. 获取一级责任部门(多个) DataRow[] firstDeptRows = dtbminfo.Select("一级Id='" + firstId + "'"); int firstDeptCount = firstDeptRows.Length; // 一级责任部门数量 // 2. 计算二级和三级指标的总行数 int secondThirdTotalRows = 0; DataRow[] secondRows = dtsan.Select("一级Id='" + firstId + "'"); foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级自身行 + 三级行数 secondThirdTotalRows += secondTotalRows; // 累加二级和三级的总行数 } // 3. 一级指标核心列的合并行数 = 一级责任部门数量 + 二级三级总行数 int firstTotalRows = firstDeptCount + secondThirdTotalRows; %> <!-- 一级指标行(核心列合并所有子行) --> <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></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> <!-- 一级责任部门分行显示(在一级指标行之后、二级指标行之前) --> <%foreach (DataRow deptRow in firstDeptRows) { string deptName = deptRow["一级指标责任部门名称"].ToString(); if (!string.IsNullOrEmpty(deptName)) { %> <tr> <td><%= deptName %></td> <!-- 第5列:一级责任部门名称 --> <td></td> <!-- 第6列:二级指标(留空,后续二级行填充) --> <td></td> <!-- 第7列:二级责任部门(留空) --> </tr> <% } }%> <% } } else { %> <tr> <td colspan="7" style="height:30px;">暂无数据</td> </tr> <%}%> </table> </div> </center> </form> </body> </html>
最新发布
07-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值