C#中Repeater控件rptEndorseList当前上下文不存在问题求助
解决嵌套Repeater控件CS0103错误:'rptEndorseList' 名称在当前上下文中不存在
刚接触开发,现有一段读取XML文件的代码,绑定嵌套Repeater控件rptEndorseList时编译器抛出CS0103错误,提示该名称在当前上下文中不存在。以下是后端C#代码和ASPX前端代码:
原C#代码
if (objRecord.LicenseHistoryList != null && objRecord.LicenseHistoryList.Length > 0 && txtLicenseOriginallyIssued.Text == "") { rptLicHistory.DataSource = objRecord.LicenseHistoryList; rptLicHistory.DataBind(); foreach(RepeaterItem objItem in rptLicHistory.Items) { var objHistoryItem = objRecord.LicenseHistoryList[objItem.ItemIndex]; ((TextBox)objItem.FindControl("txlicenseHistoryType")).Text = objHistoryItem.Type; ((TextBox)objItem.FindControl("txlicenseHistoryIssued")).Text = MVRDateToString(objHistoryItem.IssueDate); if (objHistoryItem.EndorsementList != null && objHistoryItem.EndorsementList.Length > 0) { rptEndorseList.DataSource = objHistoryItem.EndorsementList; // 错误:CS0103: The name 'rptEndorseList' does not exist in the current context rptEndorseList.DataBind(); foreach(RepeaterItem endorseItems in rptEndorseList.Items) { var objEndorseItem = objHistoryItem.EndorsementList[endorseItems.ItemIndex]; ((TextBox)objItem.FindControl("txtLicenseHistoryEndorement")).Text = objEndorseItem.EndorsementItem; } } } } else { divHistoryList.Visible = false; lblHistoryNoData.Visible = true; }
原ASPX代码
<div class="section"> <h2>License and Permit History</h2> <div id="divHistoryList" runat="server"> <asp:Repeater ID="rptLicHistory" runat="server"> <ItemTemplate> <div style="display:flex;width: 100%;align-items: center;"> <div style="width:60px;">License:</div> <div style="width:170px;"><asp:TextBox ID="txlicenseHistoryType" runat="server" style="border:none;"/></div> <div style="width:50px">Issue:</div> <div style="width:110px;"><asp:TextBox ID="txlicenseHistoryIssued" runat="server" style="border:none;"/></div> <div style="width:50px;">Expire:</div> </div> <div style="display:flex;width: 100%;align-items: center;"> <div style="width:228px;"></div> <div style="padding-right:5px;">Class:</div> <div style="flex:1;"><asp:TextBox ID="txLicenseHistoryClass" runat="server" style="border:none"/> </div> </div> <asp:Repeater ID="rptEndorseList" runat="server"> <ItemTemplate> <asp:TextBox ID="txtLicenseHistoryEndorement" runat="server" style="border:none"/> </div> </ItemTemplate> <SeparatorTemplate> <div style="height: 30px;"> </div> </SeparatorTemplate> </asp:Repeater> </ItemTemplate> <SeparatorTemplate> <div style="height: 30px;"> </div> </SeparatorTemplate> </asp:Repeater> </div> <asp:Label ID="lblHistoryNoData" runat="server" data-en="<p>No License History Was Returned.</p>" data-es="" Visible="false" /> </div>
问题核心原因
rptEndorseList是嵌套在rptLicHistory的ItemTemplate内部的子控件,不属于页面的直接控件集合,因此无法在页面级直接通过名称访问,必须通过父Repeater的RepeaterItem对象去查找。
修正后的C#代码
if (objRecord.LicenseHistoryList != null && objRecord.LicenseHistoryList.Length > 0 && txtLicenseOriginallyIssued.Text == "") { rptLicHistory.DataSource = objRecord.LicenseHistoryList; rptLicHistory.DataBind(); foreach(RepeaterItem objItem in rptLicHistory.Items) { var objHistoryItem = objRecord.LicenseHistoryList[objItem.ItemIndex]; ((TextBox)objItem.FindControl("txlicenseHistoryType")).Text = objHistoryItem.Type; ((TextBox)objItem.FindControl("txlicenseHistoryIssued")).Text = MVRDateToString(objHistoryItem.IssueDate); if (objHistoryItem.EndorsementList != null && objHistoryItem.EndorsementList.Length > 0) { // 从当前父RepeaterItem中查找子Repeater Repeater rptEndorseList = (Repeater)objItem.FindControl("rptEndorseList"); if(rptEndorseList != null) { rptEndorseList.DataSource = objHistoryItem.EndorsementList; rptEndorseList.DataBind(); foreach(RepeaterItem endorseItem in rptEndorseList.Items) { var objEndorseItem = objHistoryItem.EndorsementList[endorseItem.ItemIndex]; // 从当前子RepeaterItem中查找对应的TextBox(原代码错误使用父Item查找,会导致所有子项覆盖同一个控件值) ((TextBox)endorseItem.FindControl("txtLicenseHistoryEndorement")).Text = objEndorseItem.EndorsementItem; } } } } } else { divHistoryList.Visible = false; lblHistoryNoData.Visible = true; }
额外注意事项
- 必须添加
null判断:防止FindControl找不到控件时触发空引用异常 - 子控件赋值要对应:原代码中用父
RepeaterItem查找子Repeater内的TextBox,会导致所有子项修改同一个控件的值,必须改为从子Repeater的Item中查找 - 保持ID拼写一致:确保ASPX中控件ID和后端代码中的拼写、大小写一致(ASP.NET默认不区分大小写,但统一写法可避免潜在问题)
内容的提问来源于stack exchange,提问作者Elcid_91
相关产品推荐
相关产品推荐

