【问题标题】:Set the values to Null if its a 0 on Data Grid asp.net如果 Data Grid asp.net 上的值为 0,则将值设置为 Null
【发布时间】:2017-11-21 20:25:42
【问题描述】:

我有一个数据网格,其中包含带有标签的项目模板:

       <asp:datagrid id="ID" runat="server" Width="641px" CellPadding="2" PageSize="2"  DataKeyField="IDs"
                            AutoGenerateColumns="False" ShowFooter="True" BorderColor="AliceBlue" OnItemDataBound="ID_ItemDataBound" >
      <SelectedItemStyle ForeColor="HighlightText" BackColor="Highlight">   </SelectedItemStyle>
           <AlternatingItemStyle BackColor="WhiteSmoke">
        </AlternatingItemStyle>
                            <HeaderStyle Font-Bold="True" BackColor="AliceBlue">
        </HeaderStyle>
                            <FooterStyle Font-Bold="True" BackColor="AliceBlue">
        </FooterStyle>
                            <Columns>
            <ItemTemplate>
<asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>'   runat="server" ID="dataScore" Text='<%# DataBinder.Eval(Container, "DataItem.dataScore" ) %>'>
     </asp:label>
    <asp:label BorderStyle=None Text='<%# GetCompleteIncomplete(Convert.ToInt32(DataBinder.Eval(Container, "DataItem.dataScore")!=null)) %>' Visible='<%# DataBinder.Eval(Container, "DataItem.IsCompleteOrNot") %>' id="txtIsComplete" runat="server">
        </asp:label>
            </ItemTemplate>
            </columns>

我正在尝试将 0 设置为空白,而不是在 DataGrid 中显示 0,因此在 ItemData 绑定时我得到这样的标签并尝试将值设置为 null:

if ((e.Item.ItemType == ListItemType.Item) ||
          (e.Item.ItemType == ListItemType.AlternatingItem))
        {

            Label dataScore = (Label)e.Item.FindControl("dataScore"); // Gets that Label
            Label txtIsComplete = (Label)e.Item.FindControl("txtIsComplete");



 if(dataScore .Text == "0")
            {
                dataScore.Text = string.Empty; // Tried 
            }

【问题讨论】:

    标签: c# asp.net datagrid label itemdatabound


    【解决方案1】:

    你可以写一个函数来分析DataBinder.Eval的值

    Private String MyFunction(String value)
    {
        Return value == "0" ? String.Empty : value;
    }
    
    <asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>'   runat="server" ID="dataScore" Text='<%# MyFunction(DataBinder.Eval(Container, "DataItem.dataScore" )) %>'>
    

    【讨论】:

    • 让我试一试
    • 它说 MyFunction 由于其保护级别而无法访问,当我尝试将 Private 设置为 Protected 时,它会给出整行的错误 Error Can not convert Object to string
    • 可以直接使用三元运算符:
    • 所以问题是比较。这应该工作:
    • 不,这里有什么问题似乎没有任何工作我仍然看到 0 不是空白
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多