如何在gridview中以编程方式添加更复杂的列

时间:2022-06-29 08:27:43

I have the following columg:

我有以下专栏:

<Columns>
<asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
        <asp:ImageButton ID="imbDelete" runat="server" ImageUrl="~/images/icon_Delete.png"
            OnClientClick="return confirm('Delete attachment?');" CommandName="Delete"
            CommandArgument='<%# Eval("Name") %>' />
    </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Path" HeaderText="Path" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />

That I must insert programmatically in my code using C#. Can someone please help? I don't know how to insert ImageButton in a TemplateField or even insert all this data at once.

我必须使用c#以编程方式插入代码。有人能帮忙吗?我不知道如何在TemplateField中插入ImageButton,甚至不知道如何立即插入所有这些数据。

2 个解决方案

#1


2  

Rather than dynamically inserting it, I'd recommend setting Visible="False" on it, and in code set it to true when you want to show it. It will be much easier to manage then; for instance, in RowDataBound event handler, you can do:

与其动态地插入它,我建议在它上面设置可见的="False",并在代码中设置为true,当您想要显示它时。这样管理起来会容易得多;例如,在RowDataBound事件处理程序中,您可以:

GridView_RowDataBound(..)
{
    var deleteButton = e.Row[0].FindControl("imbDelete");
    deleteButton.Visible = (some condition);
}

#2


0  

i've ever do that with an asp button and using a CssClass for the picture.

我曾经用一个asp按钮和一个CssClass为图片做过这样的事情。

 <Columns>
<asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
        <asp:Button ID="imbDelete" runat="server" CssClass="buttonDelete"
            OnClientClick="return confirm('Delete attachment?');" CommandName="Delete"
            CommandArgument='<%# Eval("Name") %>' />
    </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Path" HeaderText="Path" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />

then in your CssClass :

然后在你的CssClass:

.buttonDelete
{
border:none;
background:url(../../images/buttonDelete.png) no-repeat;
}

if you want you can change the picture on hover like that :

如果你想改变鼠标悬停的图片:

.buttonDelete:hover
{
background:url(../../images/buttonDelete-hover.png) no-repeat;
}

#1


2  

Rather than dynamically inserting it, I'd recommend setting Visible="False" on it, and in code set it to true when you want to show it. It will be much easier to manage then; for instance, in RowDataBound event handler, you can do:

与其动态地插入它,我建议在它上面设置可见的="False",并在代码中设置为true,当您想要显示它时。这样管理起来会容易得多;例如,在RowDataBound事件处理程序中,您可以:

GridView_RowDataBound(..)
{
    var deleteButton = e.Row[0].FindControl("imbDelete");
    deleteButton.Visible = (some condition);
}

#2


0  

i've ever do that with an asp button and using a CssClass for the picture.

我曾经用一个asp按钮和一个CssClass为图片做过这样的事情。

 <Columns>
<asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
        <asp:Button ID="imbDelete" runat="server" CssClass="buttonDelete"
            OnClientClick="return confirm('Delete attachment?');" CommandName="Delete"
            CommandArgument='<%# Eval("Name") %>' />
    </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Path" HeaderText="Path" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />

then in your CssClass :

然后在你的CssClass:

.buttonDelete
{
border:none;
background:url(../../images/buttonDelete.png) no-repeat;
}

if you want you can change the picture on hover like that :

如果你想改变鼠标悬停的图片:

.buttonDelete:hover
{
background:url(../../images/buttonDelete-hover.png) no-repeat;
}