首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Textbox上的事件Gridview RowDeleting中将null转换为字符串

如何在Textbox上的事件Gridview RowDeleting中将null转换为字符串
EN

Stack Overflow用户
提问于 2015-06-29 14:46:33
回答 1查看 179关注 0票数 0

我希望在Textbox上的事件Gridview RowDeleting中将值null转换为字符串。但错误“对象引用没有设置为对象的实例”

代码隐藏:

代码语言:javascript
复制
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.
            }  
}

提前谢谢。;)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-29 14:55:50

这个错误准确地说明了正在发生的事情。您正在尝试访问和设置空对象的属性。

若要将String.Empty设置为对象的文本,请执行以下操作。只需使用空文本创建对象的新实例即可。

代码语言:javascript
复制
    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,因此可以将其简化为

代码语言:javascript
复制
txtCountryRate_Te = new TextBox();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31118723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档