사용자 입력을위한 27 개의 DropDownLists가 포함 된 표가 있습니다. 내 표는이 HTML의 27 개 항목을 가지고 : 스팬이 S1, S2, ..., S27를 색인하고 자리가 P1, P2, ..., P27를 색인여러 개의 DropDownLists 만들기
<span id="s1" runat="server"><asp:PlaceHolder ID="p1" runat="server"></asp:PlaceHolder></span>
. 스팬이 인덱싱되는 이유는 DropDownList를 어떤 선택 으로든 바꿀 수 있기 때문입니다. 즉, DropDownList가 사라집니다. 여기
내가 DropDownLists를 생성하고 방법입니다
protected void Page_Load(object sender, EventArgs e)
{
var data = CreateDataSource();
int x;
for (x = 1; x <= 27; x++)
{
DropDownList dl = new DropDownList();
string index = x.ToString();
dl.ID = "TrendList" + index;
dl.AutoPostBack = true;
dl.SelectedIndexChanged += new EventHandler(this.Selection_Change);
dl.DataSource = data;
dl.DataTextField = "TrendTextField";
dl.DataValueField = "TrendValueField";
dl.DataBind();
if (!IsPostBack)
{
dl.SelectedIndex = 0;
}
PlaceHolder ph = (PlaceHolder)form1.FindControl("p" + index);
ph.Controls.Add(dl);
}
}
런타임 오류가 마지막 줄에서 발생한다. 내가 원하는 DropDownList로를 선택하고 선택을하지만 두 번째 DropDownList로를 선택하고 선택을 할 때이 오류를 얻을 수 있습니다 :
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 46: }
Line 47: PlaceHolder ph = (PlaceHolder)form1.FindControl("p" + index);
Line 48: ph.Controls.Add(dl);
Line 49: }
이 내가 무력으로 그 일을 할 때 작동하는 것 같았다 :
를p1.Controls.Add(DropList1);
p2.Controls.Add(DropList2);
etc....
하지만 이제 오류가 발생합니다. 이 디버거에서 실행할 수 있지만 null 참조를 찾을 수 없습니다.
모든 조언을 주시면 감사하겠습니다.
감사합니다.
감사합니다. 나는 이것을 시도하고 같은 널 포인터 예외가있다. – Kevin
@Kevin - 전체 마크 업 (모든 형식 1, 어쨌든)을 게시하려면 올바른 코드를 찾아 볼 수 있는지 살펴 보겠습니다. – hmqcnoesy