空白 Gridview 单元格填充“&nbsp”进入文本框

发布于 2024-09-30 17:09:40 字数 549 浏览 4 评论 0原文

我注意到,当我从 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();
    }




}

I've noticed that when i populate textboxes from a selected row in a gridview that if the field is blank it displays " " in the textbox.

Here is the solution I came up with. I check each cell before adding it to the textbox.

I get the feeling that I'm either doing something wrong to have this problem in the first place or that there is a better way to handle this.

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();
    }




}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

分分钟 2024-10-07 17:09:41

仍然是一个小黑客,但可能比处理   更好。您可以在 GridView 列 上设置 NullDisplayText=" " ,然后使用条件,例如:

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

在本例中,没有 & nbsp; 首先。

Still a minor hack, but probably better than dealing with  . You can set NullDisplayText=" " on GridView column <asp:BoundField> and then use condition like for example:

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

In this case, there is no   to begin with.

唠甜嗑 2024-10-07 17:09:41
row.Cells[1].Text.Trim()

不适用于  ,请替换它:

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

is not working for  , replace it instead:

row.Cells[1].Text.Replace(" ", "")
夏日浅笑〃 2024-10-07 17:09:41

这也有效。在您的 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;
                }

This works too. Add this piece of code under your rowDataBound event

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;
                }
凉栀 2024-10-07 17:09:41

使用

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

use

txtEditCust_ID.Text = Server.HtmlDecode(row.Cells[1].Text.Trim());
二智少女 2024-10-07 17:09:41

删除 if 语句,只需使用:

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

您正在修剪它,因此它应该删除  

Remove the if statement, just use:

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

You are trimming it so it should remove the   anyway.

一页 2024-10-07 17:09:41
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Pager) 

这会删除为我处理   的页眉、页脚和分页器(如果您正在使用)行。

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

This removes the header, footer, and pager (if you are using) rows which took care of the   for me.

流殇 2024-10-07 17:09:41

如果要检查 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
}

If you want to check the gridview cell value whether empty or null, use this:

string decodeCellValue = Context.Server.HtmlDecode(e.Row.Cells[i].Text).Trim();
if(string.IsNullOrEmpty(decodeCellValue))
{
  // Cell value empty or NULL
}
else
{
  // Have some value
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文