ASP.Net 绑定到 Gridview 剥离一些空间(空白字符) [英] ASP.Net Binding to Gridview Strips some space (whitespace characters)

查看:13
本文介绍了ASP.Net 绑定到 Gridview 剥离一些空间(空白字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Oracle 数据库中检索数据并将其绑定到 gridview 控件.

I am retrieving data from an Oracle database and binding the same to a gridview control.

我注意到在某些情况下,列包含单引号或双引号时,空格或空白字符会被去除.

I noticed that there are instances when the column contains a single quote or double quote, the spaces or whitespace characters get stripped off.

Oracle 中字段的部分数据示例:

Sample of some data in fields in Oracle:

尽快淘汰'",

检索时,它变成...

尽快淘汰'",

还有一个...

它的测试记录"DDD" FFF

IT''S TEST RECORD" DDD " FFF

变成

它的测试记录"DDD" FFF

IT''S TEST RECORD" DDD " FFF

我不知道为什么会发生这种情况...

I don't have any clue why this is happening...

有什么想法吗?

我认为即使在这里空间也在被修剪.在我的第一个字段中的示例中:

I think even here the spaces are getting trimmed. In my example in the first field which is:

尽快淘汰'"

单引号后面实际上有两个空格,但这里只显示一个空格..奇怪...

there are actually two spaces after the single quote but it is displaying just a single space here.. Odd...

单引号空格空格双引号 --> ' "

single quote space space double quote --> ' "

我认为 asp.net 删除了引号或单引号后的多余空格??

I think the extra space after a quote or single quote is being removed by asp.net??

我还发现,当我编辑我的 gridview 时,额外的空格会被保留,但是当我回到原始视图时,空格不见了.

I also found out that when I am editing my gridview, the extra white spaces are retained but when I go back to the original view, the whitespaces are gone.

重新表述这个问题..

如何在显示数据时保留gridview中的空白?

How Do I Preserve the WhiteSpace in the gridview when displaying the data?

推荐答案

这不是 ASP.Net 的东西,而是 HTML 解析的东西.如果您要创建一个带有 div 标记的普通 Jane HTML 页面,然后在开始标记和结束标记之间放置 100 个空格,则所有这些都将被压缩为一个空格.

This is not an ASP.Net thing, it is an HTML parsing thing. If you were to create a plain Jane HTML page with a div tag in it, and then put 100 spaces between the opening and closing tag it would all be condensed into a single space.

这是一个经典的网络问题.如果您真的想让所有内容都正确显示,那么您需要先对所有空格进行 HTML 编码,然后才能在页面上显示它们.

This is a classic web issue. If you really want to have everything come out correctly, then you will need to HTML encode any spaces before displaying them on the page.

尝试用  

这里有一篇文章更深入地解释了这一点:http://webdesign.about.com/od/beginningtutorials/f/blfaqwhitespace.htm

Here is an article that explains this a little more in-depth: http://webdesign.about.com/od/beginningtutorials/f/blfaqwhitespace.htm

回答评论中的问题:

如果您需要对进入您的网格的确切内容进行大量控制,即使它是 DataBound,您可以简单地处理 RowDataBound 事件,在每行绑定后触发.

If you need to have a lot of control over exactly what is going out to your grid even when it is DataBound, you can simply handle the RowDataBound event that fires after each row is bound.

假设您有一个如下所示的 GridView:

Suppose you have a GridView that looks like this:

<asp:GridView ID="gbGridWithSpaces" AutoGenerateColumns="false" runat="server" 
    onrowdatabound="gbGridWithSpaces_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ItemWithSpaces" HeaderText="Item With Spaces" />
    </Columns>
</asp:GridView>

在您后面的代码中,您可以处理该事件以根据需要重新格式化传出文本.例如:

In your code behind, you can handle the event to re-format the outgoing text however you please. For example:

protected void gbGridWithSpaces_RowDataBound(object sender, GridViewRowEventArgs e)
{
    foreach (TableCell cell in e.Row.Cells)
    {
        cell.Text = cell.Text.Replace(" ", "&nbsp;");
    }
}

这将有效地用 &nbsp; 替换所有空格,即使在呈现到浏览器时也会保留它们.请记住,您的数据也会以这种方式返回,因此如果您打算将此信息传递回持久存储,则需要在服务器上处理此问题.

That would effectively replace all spaces with &nbsp; and preserve them even when rendered to the browser. Just remember your data will come back this way as well, so you will need to handle this on the server if you plan to pass this information back into persistent storage.

这篇关于ASP.Net 绑定到 Gridview 剥离一些空间(空白字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆