在aspx gridView

时间:2015-09-16 19:59:01

标签: c# asp.net gridview

我在'gridview'中的这个专栏中得到了这个错误。 : -

    <asp:TemplateField>
                <ItemTemplate>
                    <a href='<%# Eval("url") %>' target="_blank" style="color:#330000; font-size:18px; font-weight:bold"><%# Eval("title") %></a>
                    <br />
                    <p  runat="server" id="description"><%# Eval("des") %></p>
                    <asp:ImageButton ID="click_like" runat="server" ImageUrl= "~/pics/like.png" Height="20px" CommandName="like" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("like_enabled") %>' />&nbsp;
                    <asp:Label ID="lbl_like" runat="server" Text='<%# Eval("like") %>'></asp:Label> &nbsp;&nbsp;
                    <asp:ImageButton ID="click_dislike" runat="server" ImageUrl="~/pics/dislike.png" Height="20px" CommandName="dislike" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("dislike_enabled") %>'/>&nbsp;
                    <asp:Label ID="lbl_dislike" runat="server" Text='<%# Eval("dislike") %>'></asp:Label>
                    <p runat="server" id="labels"><%# Eval("labels") %></p>
                </ItemTemplate>

            </asp:TemplateField>

我收到了这个错误: -

    <asp:ImageButton ID="click_like" runat="server" ImageUrl= "~/pics/like.png" Height="20px" CommandName="like" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("like_enabled") %>' />&nbsp;

我得到了这个,因为我绑定了Enabled DataTable属性的值like_enabled列,其中123.005 123.0 列是&#34; true&#34;或&#34; false&#34;

1 个答案:

答案 0 :(得分:0)

您无法像这样绑定Enabled,您必须使用RowDataBound事件:

<asp:GridView id="myGrid" OnRowDataBound="myGrid_RowDatabound">

代码背后:

public void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        var enabled = (bool)DataBinder.Eval(e.Row.DataItem, "like_enabled");
        var click_like = e.Row.FindControl("click_like") as ImageButton;
        click_like.Enabled = enabled;
    }
}