C# 中 FindControl 方法及使用

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>
原文地址:https://www.cnblogs.com/Tanghongchang/p/10607935.html