데이터베이스의 행 수를 기반으로 데이터로 채워진 확인란 목록을 동적으로 만드는 웹 양식이 있습니다. 그런 다음 표시 할 자리 표시 자에 추가됩니다.페이지 포스트 백 중에 null 참조 예외 오류가 발생하여 자리 표시 자 컨트롤에서 "CheckBoxList"가 동적으로 생성되었습니다.
버튼을 클릭하면 선택된 체크 상자 값이 데이터베이스에 추가되지만 버튼을 클릭 할 때와 포스트 백 이후에는 페이지에 "System.NullReferenceException"오류가 표시됩니다. 동적 checkboxlist의 수를 생성합니다 다음 코드 (! Page.IsNotPostBack)와 루프를 Page_Load에 기록
: 그것은 보인다, 수
CheckBoxLis chkContent = new CheckBoxList();
chkContent.ID = chkIDString; //chkIDString is an incremental int based on the row count
chkContent.RepeatDirection = RepeatDirection.Horizontal;
foreach (List<t> contentList in List<t>) //data retrieved as List<t> using LINQ
{
ListItem contents = new ListItem();
contents.Text = contentList.Title;
contents.Value = contentList.contentID.ToString();
chkContent.Items.Add(contents);
}
plcSchool.Controls.Add(chkContent); //plcSchool is my placeholder
plcSchool.Controls.Add(new LiteralControl("<br>"));
protected void btnAdd_Click(object sender, EventArgs e)
{
CheckBoxList cbl = Page.FindControl("chkContent4") as CheckBoxList;
Response.Write(cbl.SelectedValue.ToString()); // now im just testing to get the value from one of the checkboxlist
}
누구나 도움을 제어하지 않습니다 다시 게시 이후에 다시 작성되므로 컨트롤을 찾고 null 참조 예외를 throw 할 수 없습니다.
감사 :
이
는 항상 이러한 유형의 질문으로 이동 페이지입니다. – newName