读书人

C# 未将对象引用设置到对象的实例,该

发布时间: 2012-01-28 22:06:13 作者: rapoo

C# 未将对象引用设置到对象的实例
我想设置 Web的主题,2 个主题myfirst 和 mysecond ,Dropdownlist1 有 2个ListItem ,text为myfirst和mysecond, vlaue也为 myfirst 和mysecond,选择 myfirst这个Item的时候就显示主题myfirst ,选择..就显示..可是选择后要设置DropdownList的选中项 所以定义了 ListItem item = DropDownList1.Items.FindByText(theme);可编译提示“未将对象引用设置到对象的实例” 高手帮忙~!!!!感恩~!!{{{在线等}}}

public partial class MSSS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

void Page_PreInit(object sender, EventArgs e)
{
string theme;
if (Request.QueryString[ "theme "] == null)
{
theme = "myfirst ";
}
else
{
theme=Request.QueryString[ "theme "];

}
Page.Theme = theme;
ListItem item = DropDownList1.Items.FindByText(theme);

if (item != null)
{
item.Selected = true;
}
}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{

string id = TreeView1.SelectedValue;

MultiView1.ActiveViewIndex =Convert.ToInt16(id);


}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string Url = Request.Path + "?theme= " + DropDownList1.SelectedItem.Value;
Response.Redirect(Url);
}


}

[解决办法]
检查声明``
检查变量是否为空
[解决办法]
string theme=null;


错误在那个地方啊?
[解决办法]
单步跟一下吧,一般是因为某个变量对象在运行时被置为了null

读书人网 >C#

热点推荐