Asp.net 将GridView列2的SQL列值格式化为用符号分隔

Asp.net 将GridView列2的SQL列值格式化为用符号分隔,asp.net,sql,gridview,eval,templatefield,Asp.net,Sql,Gridview,Eval,Templatefield,我有一个GridView,其中有一列包含来自SQL的两个值,分别来自列Author和Author2。 在我的表中,只有一行在两列中都有值,其他行只有一个Author和一个NULL。我只想用符号“&”来区分两位作者 我尝试过几种方法,第一种是使用CSS: <head> <style> .label2css:before { content: "& "; } </style> </head> .label2css:之前{ 内容:&;

我有一个GridView,其中有一列包含来自SQL的两个值,分别来自列Author和Author2。 在我的表中,只有一行在两列中都有值,其他行只有一个Author和一个NULL。我只想用符号“&”来区分两位作者

我尝试过几种方法,第一种是使用CSS:

<head>
<style> 
.label2css:before {
content: "& ";
}
</style>  
</head>

.label2css:之前{
内容:&;
}


另一个:

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Author") %>'></asp:Label> 
<asp:Label ID="Label2" runat="server" Text='<%# "&" + Eval("Author2") %>'>   </asp:Label>
</ItemTemplate>

但两者都导致了这一点:
作者&
作者和作者2
作者&

但我希望能够做到这一点:
作者
作者和作者2
作者

有办法吗?

我想这样做:

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Author") %>'></asp:Label> 
    <asp:Label ID="Label2" runat="server" Text='<%#  String.IsNullOrEmpty(Eval("Author2") as string) ? "" : Eval("Author2", "& {0}") %>'>   </asp:Label>
</ItemTemplate>
protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = SqlDataSource1;
    GridView1.DataBind();
}

这是我的测试代码。

标记:

<asp:GridView ID="GridView1" AutoGenerateColumns="false"  runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Author") %>'></asp:Label>
                <asp:Label ID="Label2" runat="server" Text='<%# String.IsNullOrEmpty(Eval("Author2") as string) ? "" : Eval("Author2", "& {0}") %>'>   </asp:Label>
                </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

守则:

public partial class WebForm3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        GridView1.DataSource = new List<MyClass>(){
            new MyClass { Author="Author"},
            new MyClass { Author="Author", Author2="Author2"},
            new MyClass { Author="Author"}

        };

        GridView1.DataBind();
    }
}

public class MyClass
{
    public string Author { get; set; }
    public string Author2 { get; set; }
}
public分部类WebForm3:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
GridView1.DataSource=新列表(){
新MyClass{Author=“Author”},
新MyClass{Author=“Author”,Author2=“Author2”},
新MyClass{Author=“Author”}
};
GridView1.DataBind();
}
}
公共类MyClass
{
公共字符串作者{get;set;}
公共字符串Author2{get;set;}
}
以下是输出:


谢谢你,@afzalugh!我可以根据您的代码这样做:

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Author") %>'></asp:Label> 
    <asp:Label ID="Label2" runat="server" Text='<%#  String.IsNullOrEmpty(Eval("Author2") as string) ? "" : Eval("Author2", "& {0}") %>'>   </asp:Label>
</ItemTemplate>
protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = SqlDataSource1;
    GridView1.DataBind();
}

如果我没记错的话,应该有一种方法可以使用databind事件来操作输出。通过这种方式,您可以在代码隐藏中检查null,并根据需要设置author2的值。