Iter Lsic Iealf
Iter Lsic Iealf

Reputation: 143

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.

Upvotes: 1

Views: 884

Answers (2)

VDWWD
VDWWD

Reputation: 35514

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()) %>'

Upvotes: 1

eldu_gate
eldu_gate

Reputation: 26

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

Upvotes: 1

Related Questions