FindControl 的使用方法
FindControl (String id): 在頁命名容器中搜索帶指定標識符的服務器控件。(有點類似javascript中的getElementById(string))
今天做了一個打印的報表 ,要求在指定位置顯示列表中某字段的內容,開始時先查詢出數據列表再每個進行判斷然后賦值,太麻煩太啰嗦,現在知道了 FindControl 方法 用這個方法覺得比之前高級多了
后台代碼
projectReviewCommentTable = municipalProjectBLL.GetCommentDTByProject(UserInfo, (string)this.ViewState["entityId"]);
foreach (DataRow dr in projectReviewCommentTable.Rows) { Label labelReviewer = FindControl("lblCertificateReviewer_" + dr[MunicipalProjectReviewCommentTable.FieldSpecialtyCode]) as Label; if (labelReviewer != null) { labelReviewer.Text = dr[MunicipalProjectReviewCommentTable.FieldReviewer].ToString(); } Label labelChecker = FindControl("lblCertificateChecker_" + dr[MunicipalProjectReviewCommentTable.FieldSpecialtyCode]) as Label; if (labelChecker != null) { labelChecker.Text = dr[MunicipalProjectReviewCommentTable.FieldChecker].ToString(); } }
需要頁面控件配合
<div class="Textbox6"> <%--道路審查人--%> <asp:Label ID="lblCertificateReviewer_LW" runat="server"></asp:Label> </div> <div class="Textbox10"> <%--道路校審人--%> <asp:Label ID="lblCertificateChecker_LW" runat="server"></asp:Label> </div> <div class="Textbox7"> <%--橋梁隧道 審查人--%> <asp:Label ID="lblCertificateReviewer_SD" runat="server"></asp:Label> </div> <div class="Textbox11"> <%--橋梁隧道 校審人--%> <asp:Label ID="lblCertificateChecker_SD" runat="server"></asp:Label> </div> <div class="Textbox15"> <%--給排水 審查人--%> <asp:Label ID="lblCertificateReviewer_JS" runat="server"></asp:Label> <asp:Label ID="lblCertificateReviewer_PS" runat="server"></asp:Label> </div> <div class="Textbox16"> <%--給排水 校審人--%> <asp:Label ID="lblCertificateChecker_JS" runat="server"></asp:Label> <asp:Label ID="lblCertificateChecker_PS" runat="server"></asp:Label> </div> <div class="Textbox5"> <%--環境 審查人--%> <asp:Label ID="lblCertificateReviewer_HJ" runat="server"></asp:Label> </div> <div class="Textbox9"> <%--環境 校審人--%> <asp:Label ID="lblCertificateChecker_HJ" runat="server"></asp:Label> </div> <div class="Textbox8"> <%--燃氣 熱力 審查人--%> <asp:Label ID="lblCertificateReviewer_RQ" runat="server"></asp:Label> <asp:Label ID="lblCertificateReviewer_RL" runat="server"></asp:Label> </div> <div class="Textbox12"> <%--燃氣 熱力 校審人--%> <asp:Label ID="lblCertificateChecker_RQ" runat="server"></asp:Label> <asp:Label ID="lblCertificateChecker_RL" runat="server"></asp:Label> </div>