2017-09-13 3 views
1

GridView에 Lables가 포함되어 있습니다. 데이터를 기반으로 Lables를 표시하거나 숨길 필요가 있습니다.GridView에서 DataBinder.Eval을 사용하는 방법은 무엇입니까?

<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를 사용하여 경계 데이터를 확인해야합니다에서 : 여기

내의 GridView입니까?

답변

0

사용 DataBinder.Eval와 RowDataBound 이벤트에서이 얻을 수있는 레이블 :

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") %>'입니다.