繁体   English   中英

如果条件在 C# ASP.NET 中,则在 gridview 中设置文本框控件

[英]Set textbox control in gridview if condition in C# ASP.NET

在我的 ASP.NET gridview 中,我默认插入了 Label 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)
        {
            ????
        }
    }
}

帮我做。

您可以使用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()) %>'

尝试添加两个控件并仅在字段“tannotation”时显示 label!=“准备好”

代码应该与此类似:

    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;
              }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM