【问题标题】:Set textbox control in gridview if condition in C# ASP.NET如果 C# ASP.NET 中的条件在 gridview 中设置文本框控件
【发布时间】:2021-04-19 20:55:02
【问题描述】:

在我的 ASP.NET 网格视图中,我已将标签 lbannotation 作为默认值插入:

<asp:TemplateField HeaderText="annotation"
   ItemStyle-HorizontalAlign="Left">
   <ItemTemplate>
      <asp:Label ID="lbannotation" runat="server"
          Text='<%# Eval("tannotation").ToString() %>'
          CssClass="ddl_Class_new"></asp:Label>
  </ItemTemplate>
</asp:TemplateField>

如果是数据库表列的字符串tannotation

Eval("tannotation").ToString()

在我们的字符串中包含单词ready

我需要从asp:Label更改

<asp:TemplateField HeaderText="annotation"
   ItemStyle-HorizontalAlign="Left">
   <ItemTemplate>
      <asp:Label ID="lbannotation" runat="server"
          Text='<%# Eval("tannotation").ToString() %>'
          CssClass="ddl_Class_new"></asp:Label>
  </ItemTemplate>
</asp:TemplateField>

asp:TextBox

<asp:TemplateField HeaderText="annotation"
   ItemStyle-HorizontalAlign="Left">
   <ItemTemplate>
      <asp:TextBox ID="txannotation" runat="server"
          Text='<%# Eval("tannotation").ToString() %>'
          CssClass="ddl_Class_new"></asp:TextBox>
  </ItemTemplate>
</asp:TemplateField>

我应该为这个需要设置什么?

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem != null)
        {
            ????
        }
    }
}

帮我做。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以使用 Visible 属性内联执行此操作。使用Contains 检查“准备就绪”。它返回一个布尔值,因此您可以将其用于可见性。

    <asp:TemplateField>
        <ItemTemplate>
    
            <asp:Label ID="lbannotation" Visible='<%# Eval("tannotation").ToString().Contains("ready") %>' runat="server"></asp:Label>
    
            <asp:TextBox ID="txannotation" Visible='<%# !Eval("tannotation").ToString().Contains("ready") %>' runat="server"></asp:TextBox>
    
        </ItemTemplate>
    </asp:TemplateField>
    

    或者您可以使用更通用的方法

    public bool IsReady(string keyword, string value)
    {
        return value.Contains(keyword);
    }
    

    aspx

    Visible='<%# IsReady("ready", Eval("tannotation").ToString()) %>'
    

    【讨论】:

      【解决方案2】:

      尝试添加两个控件并仅在字段“tannotation”!=“ready”时显示标签

      代码应该是这样的:

          protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs 
      e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              if (e.Row.DataItem != null)
              {
                  var lbannotation = (Lable)e.Row.FindControl("lbannotation");
                  var txannotation = (TextBox)e.Row.FindControl("txannotation");
                  if(e.Row.DataItem["tannotation"].ToString() == "ready")
                    {
                      lbannotation.Visible = false;
                      txannotation.Visible = true;
                    }else{
                      txannotation.Visible = false;
                      lbannotation.Visible = true;
                    }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-25
        • 2016-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多