我有以下GridView和ObjectDataSource:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" />
</ItemTemplate>
</asp:TemplateField>
在我的模板字段中,我只想在屏幕截图 Id 计算结果为非空非零值时显示按钮。
如果屏幕截图 ID 为 DbNull 或 0,那么我想将单元格留空。
我已经在 RowDataBound 上尝试过这段代码,但没有成功,因为在 GridView 中我只有单元格空白,如果 screenshotId 的计算结果为非空非零值也是如此。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int screenshotId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "screenshotId"));
ImageButton btnShowImage = e.Row.FindControl("imgbtnEdit") as ImageButton;
if (screenshotId > 0)
{
btnShowImage.Visible = true;
Response.Write(screenshotId + "<br />"); //here the value are 1
}
else
{
btnShowImage.Visible = false;
}
}
}
最好的方法是什么?
这可以在网格视图中完成:
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" Visible='<%#Eval("screenshotId") == DBNull.Value ? false : true %>' />
因此,如果屏幕截图 ID 为 null,则设置控件 Visible 则为 false,否则为 true。