兩種方法:
資料繫結控制項中,判斷資料庫欄位值來決定true/false屬性值,記得要把Eval(欄位)做轉型才能比較!
如果資料庫IsActive欄位型別是數字:
<asp:ImageButton ID="btnDelete" runat="server"
ToolTip="Delete"
ImageUrl = "/images/Delete.png"
Enabled = '<%# (int)Eval("IsActive") == 0? false : true %>' />
如果資料庫IsActive欄位型別是文字(含char、varchar、nvarchar):
<asp:ImageButton ID="btnDelete" runat="server"
ToolTip="Delete"
ImageUrl = "/images/Delete.png"
Enabled = '<%# Eval("IsActive").ToString() == 'Y'? false : true %>' />
但轉Boolean卻不行,會發生"字串未被辨認為有效的 Boolean。"的錯誤:
<asp:ImageButton ID="btnDelete" runat="server"
ToolTip="Delete"
ImageUrl = "/images/Delete.png"
Enabled = '<%# Convert.ToBoolean(Eval("IsActive")) %>' />
如果資料庫欄位型別是Boolean的話,就可以直接Eval不用轉型:
<asp:ImageButton ID="btnDelete" runat="server"
ToolTip="Delete"
ImageUrl = "/images/Delete.png"
Enabled = '<%# Eval("IsActive") %>' />