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

In my ASP.NET gridview, I have insert the Label lbannotation as default:

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

If the string of database table column tannotation:
Eval("tannotation").ToString()

contains in our string the word ready

I need change from 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>

to 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>

What should I set for this need on?
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem != null)
        {
            ????
        }
    }
}

Help me to do it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can do that inline with the Visible property. Use Contains to check for “ready”. It returns a bool so you can use that for the Visibility.

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

Or you can use a Method for more general use
public bool IsReady(string keyword, string value)
{
    return value.Contains(keyword);
}

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

Method 2

Try to add both controls and show label only when field “tannotation” != “ready”

Code should be similar to this:

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x