2010-11-06 154 views
0

我注意到,当我填充GridView中选定行的文本框时,如果字段为空,它会在文本框中显示“& nbsp”。空白Gridview单元格填充文本框中的“&nbsp”

这是我提出的解决方案。在将它添加到文本框之前,我会检查每个单元格。

我感觉我要么做错了首要有这个问题,或者有更好的方法来处理这个问题。

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 

    //// Get the currently selected row using the SelectedRow property. 
    GridViewRow row = GridView1.SelectedRow; 

    // Load data from selected row into textboxes 
    if (row.Cells[1].Text.Trim() != " ") 
    { 
     txtEditCust_ID.Text = row.Cells[1].Text.Trim(); 
    } 




} 

回答

0

取出if语句,只需使用:

txtEditCust_ID.Text = row.Cells[1].Text.Trim(); 

你修剪它,所以它应该删除 反正。

+0

'System.String.Trim()'不会删除'' (文本),至少不在'2.0'中。 – Bobby 2011-11-08 12:40:21

+0

不起作用4.0并非 – 2013-06-06 19:36:04

2
row.Cells[1].Text.Trim() 

不工作 ,而不是取代它:

row.Cells[1].Text.Replace(" ", "") 
1

这工作了。添加这段代码你rowDataBound事件

if (e.Row.Cells[1].Text.Length == 0 || e.Row.Cells[1].Text.Equals(" ") || e.Row.Cells[1].Text.Equals("") || e.Row.Cells[1].Text.Equals(string.Empty)) 
       { 
        e.Row.Cells[1].Text = string.Empty; 
       } 
1

使用

txtEditCust_ID.Text = Server.HtmlDecode(row.Cells[1].Text.Trim()); 
6

尚未成年的黑客攻击之下,但可能比处理&nbsp;更好。您可以在GridView的列<asp:BoundField>设置NullDisplayText=" ",然后使用条件例如像:

if (String.IsNullOrWhiteSpace(e.Row.Cells[1].Text)) 
{ 
    // do something with e.Row 
} 

在这种情况下,没有&nbsp;开始。

0
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Pager) 

这将删除页眉,页脚和寻呼机(如果您使用),它照顾了&nbsp;为我行。

0

如果你想检查GridView的单元格的值是否为空或null,使用此:

string decodeCellValue = Context.Server.HtmlDecode(e.Row.Cells[i].Text).Trim(); 
if(string.IsNullOrEmpty(decodeCellValue)) 
{ 
    // Cell value empty or NULL 
} 
else 
{ 
    // Have some value 
} 
+1

请先编辑您的回答,请查看:http://stackoverflow.com/help/formatting – neohope 2016-09-02 07:56:58

相关问题