2017-09-13 58 views
1

我有一個GridView包含標籤,我需要顯示/隱藏基於數據的標籤。如何在GridView中使用DataBinder.Eval?

這裏是我的GridView:

<asp:GridView ID="GridView_Profiles" runat="server" CssClass="grid" HorizontalAlign="Center" 
             OnRowDataBound="GridView_Profiles_OnRowDataBound" CellSpacing="1" GridLines="None" 
             AutoGenerateColumns="False" Width="90%"> 
    <Columns> 
     <asp:Label ID="Label_SelectedCount" runat="server"> 
     <span style="width:auto;color:White;background-color:#0c95be;height:auto;margin:0px;font-size:12px;cursor:pointer;padding-left:10px;padding-right:10px;padding-top:5px;padding-bottom:5px;"> 
      <%#Eval("Count") %> 
     </span> 
     </asp:Label> 
    <asp:Label ID="lblNoCount" runat="server" Text="-"></asp:Label> 
    </Columns> 
</asp:GridView> 

在上面的GridView的RowDataBound我應該怎麼檢查使用DataBinder.Eval邊界數據?

回答

0

使用它來獲取標籤中RowDataBound事件與DataBinder.Eval

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // find your label text in gridview with DataBinder.Eval 
     string count = DataBinder.Eval(e.Row.DataItem, "Count") as string; 

     // find your label control in gridview 
     Label lb = (Label)e.Row.FindControl("Label_SelectedCount"); 

     // check condition to show/hide label (you use your own condition) 
     if(count > 0) 
      lb.Visible = true; 
     else 
      lb.Visible = false; 

    } 
} 

或者你可以用DataBinder.Eval像綁定的GridView:

<asp:TemplateField HeaderText="Count" 
    <ItemTemplate> 
     <asp:Label ID="Label_SelectedCount" runat="server" > 
      <%# DataBinder.Eval(Container.DataItem, "Count")%> 
     </asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 

注:您也可以將數據綁定到標籤的文本像這樣的屬性Text='<%#Eval("Count") %>'