如何在DataBinding上更改datalist按钮的动态css样式

时间:2014-07-18 11:44:04

标签: asp.net datalist

<asp:DataList ID="DataList1" runat="server" RepeatColumns="5"  RepeatDirection="Horizontal"
    onitemcommand="DataList1_ItemCommand"
    onselectedindexchanged="DataList1_SelectedIndexChanged"
    ondatabinding="DataList1_DataBinding">

    <ItemTemplate>
      <asp:HiddenField ID="Hdnqid" runat="server" Value='<%# Bind("Id") %>' />
        <asp:HiddenField ID="HidnResultStatus" runat="server" Value='<%# Bind("ResultStatus") %>' />

                <asp:Button ID="Butto" runat="server" Text='<%#Eval("Id") %>'  CommandName="Save&Next"
                    CommandArgument='<%#Eval("Id") %>' />
    </ItemTemplate>

</asp:DataList>

我想根据表格的值更改按钮的颜色。

1 个答案:

答案 0 :(得分:0)

这样做的一种方法是使用数据列表的OnDataBound事件。如果您可以获得按钮以及数据项属性。您可以在此处更改按钮的属性,如下所示

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    Button button = e.Item.FindControl("Butto") as Button;
    HiddenField HidnResultStatus = e.Item.FindControl("HidnResultStatus") as HiddenField;
    string property = DataBinder.Eval(e.Item.DataItem, "colorproperty") as string;
    //Here you can change the button color based on the value
    if(HidnResultStatus.Value=="") 
      button.ForeColor = System.Drawing.Color.Black;
    if(HidnResultStatus.Value=="1") 
      button.ForeColor = System.Drawing.Color.Brown;

}