从下拉列表检索值时发生运行时错误代码 [英] Runtime error code while retrving value from dropdownlist

查看:66
本文介绍了从下拉列表检索值时发生运行时错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DropDownList d = (DropDownList)Panel3.FindControl("Text" + i); value = d.SelectedValue.ToString();



你调用的对象是空的."上面的代码写在第二个按钮中.
在第一个选项中,我动态创建了dropdownlist



"Object reference not set to an instance of an object." above code is written in second button.
in first buttion i created dynamically dropdownlist

int a = Convert.ToInt32(TextBox1.Text);


for (int i = 0; i < a; i++)
{
d = new DropDownList();
d.ID = "Text" + i;
LiteralControl l1 = new LiteralControl("<br></br>");
SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
con1.Open();
d.Items.Add("anant");
d.Items.Add("Gautum");

//d.SelectedIndexChanged += new EventHandler(d_SelectedIndexChanged);


Panel3.Controls.Add(d);
Panel3.Controls.Add(l1);

con1.Close();
}

推荐答案

可能是以下两种情况之一:

Could be one of 2 things:

  1. FindControl 返回null.
  2. SelectedValue 返回null.
  1. FindControl is returning null. Add a check there.
  2. SelectedValue is returning null. Add a check there as well.


如果您尝试调试并逐步执行代码,则将获得导致错误的确切原因. 尝试修复它,您将在此过程中学到很多.
If you try debugging and step through your code, you will get to the exact cause of the error.
Try fixing it, you will learn a lot in the process as you do this.


0. imho Nishant和Abhinav的答案将为您提供准确找出问题出处的信息.

1.您可能希望用"ASP.NET"标记您的问题

2.绝对清楚第二个按钮具有对Panel3的有效引用,其中在运行时在循环中创建了DropDownLists吗?

3.一个建议:与您在循环中创建DropDownLists有关.为什么在创建它们时保留它们的有效列表以备将来参考:
0. imho Nishant and Abhinav''s answers give you exactly what you need to figure out what''s going wrong.

1. you may wish to tag your question with ''ASP.NET''

2. is it absolutely clear that the second button has a valid reference to Panel3 where the DropDownLists are created in a loop at run-time ?

3. One suggestion: related to your creating DropDownLists in a loop. Why not keep a valid List of them as they are created for future reference:
// keep a List of the created DropDownLists
public List<dropdownlist> ddList = new List<dropdownlist>();
//
for (int i = 0; i < a; i++)
{
d = new DropDownList();
d.ID = "Text" + i;
//
// add the new DropDownList
ddList.Add(d);
// ...</dropdownlist></dropdownlist>

然后,您可以根据需要通过索引号访问它们,而不是使用对"FindControl"的调用中固有的间接解析查找".

但是请记住,除了在列表声明中使用修饰符"public"之外,您可能还需要使用其他一些技巧……如果消费者位于不同的类中,则将此类型的DropDownList列表公开给消费者,容器等

为了解决这个问题,我的选择是将列表设为私有,然后通过公共属性公开该列表.

Then, you can access them by index number as needed rather than using the ''indirect parsed look-up'' inherent in the call to ''FindControl.

But, keep in mind that you may need to use some further technique ... other than using the modifier ''public'' on the List declaration ... to expose this List of Type DropDownList to consumers if they are in a different class, container, etc.

To handle that, my choice would be to make the List private, and then expose it via a Public Property.


这篇关于从下拉列表检索值时发生运行时错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆