如何在文本框上的事件网格视图 RowDeleting 中将 null 转换为字符串
How to convert null to string in Event Gridview RowDeleting on Textbox
我想在文本框上的 Event Gridview RowDeleting 中将值 null 转换为字符串。
但是报错"Object reference not set to an instance of an object."
隐藏代码:
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.
}
}
提前致谢。 ;)
错误准确地说明了发生了什么。您正在尝试访问和设置空对象的 属性。
将 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();
我想在文本框上的 Event Gridview RowDeleting 中将值 null 转换为字符串。 但是报错"Object reference not set to an instance of an object."
隐藏代码:
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.
}
}
提前致谢。 ;)
错误准确地说明了发生了什么。您正在尝试访问和设置空对象的 属性。
将 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();