Asp.net 对象引用未设置为对象的引用
我有一个列intrestedFields的dropdownlist,我在该dropdownlist上添加了ListItem属性 现在的问题是,当我将intrest_field()方法放在回发中时,ListItem属性没有显示, 当我把它放在回发之外时,listitem属性正在显示,但我的另一个dropdownlist项目没有显示,并且显示错误“Object reference not set a intance of a Object” 我尝试过使用PreRender,但同样的问题也出现了 .cs代码Asp.net 对象引用未设置为对象的引用,asp.net,c#-4.0,Asp.net,C# 4.0,我有一个列intrestedFields的dropdownlist,我在该dropdownlist上添加了ListItem属性 现在的问题是,当我将intrest_field()方法放在回发中时,ListItem属性没有显示, 当我把它放在回发之外时,listitem属性正在显示,但我的另一个dropdownlist项目没有显示,并且显示错误“Object reference not set a intance of a Object” 我尝试过使用PreRender,但同样的问题也出现了 .c
if (!IsPostBack)
{
intrest_field();
}
public void intrest_field()
{
ListItem l1 = new ListItem();
l1 = drpfield.Items.FindByValue("1");
l1.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l1.Attributes.Add("disabled", "true");
l1.Value = "-1";
ListItem l2 = drpfield.Items.FindByValue("2");
l2.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l2.Attributes.Add("disabled", "true");
l2.Value = "-1";
ListItem l3 = drpfield.Items.FindByValue("3");
l3.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l3.Attributes.Add("disabled", "true");
l3.Value = "-1";
}
.aspx代码
<asp:DropDownList ID="drpfield" runat="server" Height="20px" Width="190px"
AutoPostBack="True">
<asp:ListItem Value="1">Top Categories</asp:ListItem>
<asp:ListItem >Accounts</asp:ListItem>
<asp:ListItem Value="2">Financial </asp:ListItem>
<asp:ListItem>ITES</asp:ListItem>
<asp:ListItem Value="3">HR</asp:ListItem>
<asp:ListItem>Marketing</asp:ListItem> </asp:DropDownList>
它可以工作,但我的另一个dropdownlist项目没有显示
i、 e从drpfield中选择项目后,当我从另一个dropdownlist国家/地区选择项目时,该国家/地区未显示状态。
如果不使用dropdownlist drpfield,它可以正常工作
有什么问题吗?你做得不对。例如,您正在按值“1”查找项目,更改为“-1”,回发后,您将尝试按值“1”查找项目,该值已不存在
l1 = drpfield.Items.FindByValue("1");
... ... ...
l1.Value = "-1";
我建议不要将值更改为-1:
protected void Page_Load(object sender, EventArgs e)
{
intrest_field();
}
public void intrest_field()
{
ListItem l1 = new ListItem();
l1 = drpfield.Items.FindByValue("1");
l1.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l1.Attributes.Add("disabled", "true");
//l1.Value = "-1";
ListItem l2 = drpfield.Items.FindByValue("2");
l2.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l2.Attributes.Add("disabled", "true");
//l2.Value = "-1";
ListItem l3 = drpfield.Items.FindByValue("3");
l3.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l3.Attributes.Add("disabled", "true");
//l3.Value = "-1";
}
如果有理由将值设置为-1,那么应该有另一种方法
如果保留'l1.Value=“-1”,当然可以检查l1是否为null,但回发后它将为null,并且不会应用属性
protected void Page_Load(object sender, EventArgs e)
{
intrest_field();
}
public void intrest_field()
{
ListItem l1 = new ListItem();
l1 = drpfield.Items.FindByValue("1");
l1.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l1.Attributes.Add("disabled", "true");
//l1.Value = "-1";
ListItem l2 = drpfield.Items.FindByValue("2");
l2.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l2.Attributes.Add("disabled", "true");
//l2.Value = "-1";
ListItem l3 = drpfield.Items.FindByValue("3");
l3.Attributes.Add("style", "color:gray;font-weight:bold;font-size:larger");
l3.Attributes.Add("disabled", "true");
//l3.Value = "-1";
}