我有一個asp.net chckebox列表從查詢填充的CheckBoxList:使用FindByValue
<asp:EntityDataSource ID="GradeLevelEntityDataSource" runat="server"
ConnectionString="name=NewCourseRequestDataEntities"
DefaultContainerName="NewCourseRequestDataEntities" EnableFlattening="False"
EntitySetName="grade_levels" OrderBy="it.grade_level_index">
</asp:EntityDataSource>
<asp:CheckBoxList ID="GradeLevelCheckBoxList" runat="server" cssClass="horizontalcontrols"
DataSourceID="GradeLevelEntityDataSource"
DataTextField="grade_level_description" DataValueField="grade_level_id" AutoPostBack="True"
OnSelectedIndexChanged="CollegeInstitutionsListboxChange"
RepeatDirection="Horizontal" RepeatLayout="Flow">
</asp:CheckBoxList>
用戶可以返回頁面,它就會記錄ID,拉動記錄數據,並應選中相應的複選框:
CheckBoxList grades = (CheckBoxList)FindControl("GradeLevelCheckBoxList");
foreach (request_grade_college r in CurrentRequest.request_grade_college)
{
ListItem grade = grades.Items.FindByValue(r.grade_level_id.ToString());
if (grade != null)
{
grade.Selected = true;
}
}
代碼r.grade_level_id.ToString()的部分確實返回正確的GUID,類型爲String。但是,ListItem等級保留爲空,因此沒有任何GradeLevelCheckboxList被檢查。 我錯過了什麼?
你能向我們展示你綁定它的代碼嗎? – 2012-07-18 06:20:44
@你在哪個事件中放置了foreach循環? – 2012-07-18 07:01:50