Отформатируйте столбец GridView 2, значения столбца SQL должны быть разделены символом

У меня есть GridView с одним столбцом, который содержит 2 значения из SQL, из столбцов Author и Author2. В моей таблице есть только одна строка со значениями в обоих столбцах, в других только один Автор и NULL. Я хочу отделить только те, у которых есть оба автора, символом «&».

Я пробовал сделать это несколькими способами, первый - с помощью CSS:

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

...

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

и другой:

<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
Автор

Есть способ сделать это?


person Calm    schedule 11.01.2014    source источник
comment
Если я правильно помню, должен быть способ использовать событие привязки данных для управления выводом. Таким образом, вы можете проверить наличие null в коде позади и установить значение author2 по своему усмотрению.   -  person surfmuggle    schedule 12.01.2014


Ответы (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>

Вот мой тестовый код.

Разметка:

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

И вот результат:

введите описание изображения здесь

person afzalulh    schedule 12.01.2014

спасибо, @afzalulh! Я смог сделать это на основе вашего кода вот так:

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = SqlDataSource1;
    GridView1.DataBind();
}
person Calm    schedule 12.01.2014