0

I am getting the above error. The error is on line 33, it says.

Here's the code:

Source Error:

Line 31: 
Line 32: while(r.Read()) {
Line 33:     listBox1.Items.Add(new ListItem(r["first_name"], r["first_name"])); //this is the offending line
Line 34: }
Line 35: con.Close();

Now, I figured my ASP.NET was fine, I have the following code there:

<asp:ListBox ID="listBox1" runat="server">
</asp:ListBox>

What might I be doing incorrectly here? I have the user control setup in the .aspx file, I have the correct item in my .aspx.cs file - it should work, shouldn't it? What am I missing? I'm still new to C#

4

2 回答 2

0

如果您在 aspx 中创建了一个实例,那么您必须使用它的集合在实例UserControl中找到ListBox控件。UserControlControls

这是 MSDN 上的链接。

另一种选择是将您的个人控制处理程序放在UserControl.

于 2012-10-22T15:08:21.337 回答
0

您的 listBox1 控件是否存在于您的 Designer.cs 文件中?如果没有,请通过删除它、右键单击 aspx 文件并单击“转换为 Web 应用程序”来重新生成自动生成的 C# 文件。

于 2012-10-22T15:28:39.510 回答