버튼을 클릭하면 런타임에 드롭 다운 목록을 만들었습니다. 동적 드롭 다운 목록에서 선택한 텍스트를 가져 오기 위해 다른 버튼을 보았습니다. 드롭 다운 목록에서 선택한 텍스트를 가져 오려고하면 오류가 발생했습니다. 개체 참조가 설정되지 않은 경우, 다음은 내 코드입니다.asp.net에서 동적 드롭 다운 목록
TableRow tr;
TableCell tc;
DropDownList dp;
TextBox txt;
protected void Button1_Click(object sender, EventArgs e)
{
int no = int.Parse(TextBox1.Text);
for (int i = 0; i < no; i++)
{
tr = new TableRow();
tr.BorderStyle = BorderStyle.Groove;
for (int j = 0; j < 1; j++)
{
tc = new TableCell();
tc.BorderStyle = BorderStyle.Groove;
dp = new DropDownList();
//form1.Controls.Add(dp);
txt = new TextBox();
dp.Items.Add("hello");
tc.Controls.Add(dp);
tc.Controls.Add(txt);
tr.Cells.Add(tc);
}
Table1.Rows.Add(tr);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text =((DropDownList)this.FindControl("dp")).SelectedItem.Text;
}