Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在GridView中使用DataBinder.Eval?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何在GridView中使用DataBinder.Eval?

C# 如何在GridView中使用DataBinder.Eval?,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView包含标签,我需要根据数据显示/隐藏标签 以下是我的GridView: <asp:GridView ID="GridView_Profiles" runat="server" CssClass="grid" HorizontalAlign="Center" OnRowDataBound="GridView_Profiles_OnRowDataBound" CellSpacing="1" G

我有一个GridView包含标签,我需要根据数据显示/隐藏标签

以下是我的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
检查边界数据?

使用此选项在带有
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;

    }
}
或者可以将GridView与
DataBinder.Eval
绑定,如:

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

使用此选项获取带有
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;

    }
}
或者可以将GridView与
DataBinder.Eval
绑定,如:

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