我希望在Textbox上的事件Gridview RowDeleting中将值null转换为字符串。但错误“对象引用没有设置为对象的实例”。
代码隐藏:
protected void gvTerm_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
TextBox txtCountryRate_Te = (TextBox)gvTerm.Rows[e.RowIndex].FindControl("txtCountryRate_Te");
if (txtCountryRate_Te == null)
{
txtCountryRate_Te.Text = string.Empty; //<== Error Object reference not set to an instance of an object.
}
}提前谢谢。;)
发布于 2015-06-29 14:55:50
这个错误准确地说明了正在发生的事情。您正在尝试访问和设置空对象的属性。
若要将String.Empty设置为对象的文本,请执行以下操作。只需使用空文本创建对象的新实例即可。
protected void gvTerm_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
TextBox txtCountryRate_Te = (TextBox)gvTerm.Rows[e.RowIndex].FindControl("txtCountryRate_Te");
if (txtCountryRate_Te == null)
{
txtCountryRate_Te = new TextBox
{
Text = String.Empty
};
}
}默认值是String.Empty,因此可以将其简化为
txtCountryRate_Te = new TextBox();https://stackoverflow.com/questions/31118723
复制相似问题