在ItemDataBound上设置ItemTemplate标签的值

时间:2017-11-06 20:17:43

标签: c# asp.net datagrid label itemtemplate

我有一个带有TemplateColumn的Datagrid。在该列中,我有2个包含一些数据的标签。

以下是代码:

<asp:datagrid id="dgData" runat="server" Width="658px" CellPadding="2" PageSize="2" DataKeyField="ID"
            AutoGenerateColumns="False" ShowFooter="True" BorderColor="AliceBlue" OnItemDataBound="dgData_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>
    <asp:BoundColumn DataField="ID" Visible="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="Name" HeaderText="" ItemStyle-VerticalAlign="Top"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Term 1" ItemStyle-Wrap="True">
      <ItemTemplate>
        <asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>'   runat="server" ID="edit_Score" Text='<%# DataBinder.Eval(Container, "DataItem.Score" ) %>'>
        </asp:label>
        <asp:label BorderStyle=None Text='<%# GetCompleteIncomplete(Convert.ToInt32(DataBinder.Eval(Container, "DataItem.Score"))) %>' Visible='<%# DataBinder.Eval(Container, "DataItem.IsCompleteOrNot") %>' id="txtIsComplete" runat="server">
        </asp:label>
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:datagrid>

运行时我看到了这个: There is the Name and the Value is 0 如果Text属性为0,我想将Text设置为空值而不是显示'0'。

到目前为止,我在ItemDataBound事件上尝试了以下内容:

if ((e.Item.ItemType == ListItemType.Item) ||
              (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                var ModuleName = e.Item.Cells[1].Text;
                Label edit_Score = (Label)e.Item.FindControl("edit_Score"); // This gets the Label's values 
                Label txtIsComplete = (Label)e.Item.FindControl("txtIsComplete");
  if (edit_Score.Text == "0" ) 
            {
                // I initially tried setting the text to "" but that didn't work.
                // Setting it to null doesn't seem to work either.
                edit_Score.Text = null;

                txtIsComplete.Text = null;

                //edit_Score2.Visible = false;
            }

使用ItemDataBound事件在TemplateColumn中清空此Label控件的Text属性的正确方法是什么?

0 个答案:

没有答案