我的网络应用程序中有几个按钮,我想用它们从一个盒子添加到另一个盒子。使用以前的代码,我收到一个空引用错误
错误指向-1;在(int i = lbAppsI.Items.Count - 1; i> = 0; i--)的行中 以下
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
{
if (lbAppsI.Items[i].Selected == true)
{
lbImpactedApps.Items.Add(new ListItem(lbAppsI.Items[i].Text, lbAppsI.Items
[i].Value + ","));
}
}
}
未设置为对象实例的对象引用是抛出的错误。
整个代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Configuration;
namespace LandscapeServices
{
public partial class Update : System.Web.UI.Page
{
public string query { get; set; }
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LandscapeServicesConnectionString"].ConnectionString);
DataSet dt = new DataSet();
string[] IApps;
string[] SApps;
ListBox lbImpactedApps;
ListBox lbSupportingApps;
ListBox lbAppsI;
ListBox lbAppsS;
TextBox txtPrj;
TextBox txtArt;
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
string ImpactedApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label10"))).Text;
IApps = ImpactedApp.Split(',');
string SupportingApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label11"))).Text;
SApps = SupportingApp.Split(',');
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
lbImpactedApps = (ListBox)e.Row.FindControl("ListBox1");
lbSupportingApps = (ListBox)e.Row.FindControl("ListBox2");
lbAppsI = (ListBox)e.Row.FindControl("ListBox3");
lbAppsS = (ListBox)e.Row.FindControl("ListBox4");
txtPrj = (TextBox)e.Row.FindControl("txtProjectName");
foreach (string lst in IApps)
{
lbImpactedApps.Items.Add(lst.ToString());
//lbImpactedApps.Items.FindByText(lst.ToString()).Selected = true;
}
lbImpactedApps.DataBind();
foreach (string lst in SApps)
{
lbSupportingApps.Items.Add(lst.ToString());
//lbSupportingApps.Items.FindByText(lst.ToString()).Selected = true;
}
lbSupportingApps.DataBind();
}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
{
if (lbAppsI.Items[i].Selected == true)
{
lbImpactedApps.Items.Add(new ListItem(lbAppsI.Items[i].Text, lbAppsI.Items[i].Value + ","));
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
}
protected void Button4_Click(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
错误很明显:lbAppsI
已经过期但未初始化。因此,当您尝试读取items
对象的lbAppsI
属性为空时抛出异常
通过调试代码验证这一点,在此处设置断点
for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
通常你会得到lbAppsI=null