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;
}
I created dropdownlist at runtime when a button is clicked.and i palced another button to get the selected text from dynamic dropdownlist.When i try to retrieve the selected text from dropdownlist it gives me the error called object reference not set, following is my code.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能这样做。请记住,对于每个请求,您都会获得一个新页面对象,以及其中所有控件的新副本。动态添加的任何控件每次都必须以相同的方式添加,否则它将不存在。
在这种情况下,您可以在单击按钮时添加一次。当您单击“button2”时,会生成一个请求并创建一个新的页面对象,该对象不再包含您的下拉列表,因为它仅添加到“button1”处理程序中。
最简单的方法是将下拉列表正常添加到页面,但只需将 Visible 设置为 false。然后,当他们单击按钮 1 时,将 Visible 设置为 true。这将确保您的下拉列表始终存在。
动态控件很棘手,应该尽可能避免,特别是如果您是 ASP.Net 的新手。
You can't do it this way. Remember that on every request, you get a new page object, and new copies of all the controls in it. Any control you add dynamically must be added the same way every single time, otherwise it won't exist.
In this case, you add it once, when the button is clicked. When you click button2, a request is generated and a new page object is created that no longer has your dropdownlist, because it is only ever added in the button1 handler.
The easiest thing to do would be add your dropdownlist to the page normally but just set Visible to false. Then when they click button 1, set Visible to true. This will ensure that your dropdownlist will always be present.
Dynamic controls are tricky, and should be avoided when possible, especially if you're new to ASP.Net.
实际上,我能够让它工作。
我在创建表之前创建了一个数据集,然后:
我不是专家或任何人,我只是啄它,直到我让它做我想要的事情。
Actually, I was able to make it work..
I created a dataset ahead of the table creation, then:
I'm not an expert or anything, I just peck at it until I make it do what I want.