0
<ItemTemplate>
    <asp:TextBox ID="Q1" runat="server" Text='<%# Bind("Q1") %>'></asp:TextBox>
</ItemTemplate>
.
.
.
<ItemTemplate>
    <asp:TextBox ID="Q2" runat="server" Text='<%# Bind("Q2") %>'></asp:TextBox>
</ItemTemplate>

I currently have a page with fields as textbox and I would like to change some of them label based on condition in code behind.

For example if Window_name='Q2' --> make Q2 Q3 Q4 textbox and Q1 label if it is Window_name='Q3' make Q3 and Q4 textbox but Q1 and Q2 label

Btw, I'm not using edit/select gridview modes as I made it bulk update gridview (one button to update all rows)

5
  • you tagged as C# and vb.net. Choose one otherwise people feel compelled to provide answer with both translations. Commented Apr 16, 2014 at 4:25
  • Do you want to make a textbox look like label and on which event you want to make it? Commented Apr 16, 2014 at 4:27
  • Valamas: I do feel comfortable in both C# and vb.net and generally it is easy to convert/understand one from the other Commented Apr 16, 2014 at 4:33
  • Why do you want to change TextBox to Label? while you have ways to do so... like changing TextBox style like Label using css or creating both and showing one and hiding other based on condition... Commented Apr 16, 2014 at 5:06
  • I have been trying to show/hide with no sucess any help would be appreciated Commented Apr 16, 2014 at 5:51

1 Answer 1

2

I'm trying to help you with a sample of two controls and sample grid view ID 'GridView1' here, alter it according to your code:

You could either have the labels created instead of showing the text box in the CODE BEHIND or create both textboxes and labels initially and show them when needed.

Also instead of doing it in the Page_Load function you could do it in the 'RowDataBound' event of GridView and bind the GridView each every time a post back is done.

ASPX Code:

<ItemTemplate>
      <asp:TextBox ID="Q1" runat="server" Text='<%# Bind("Q1") %>'></asp:TextBox>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("Q1") %>' Visible="false">
      </asp:Label>
</ItemTemplate>

.....

<ItemTemplate>
      <asp:TextBox ID="Q2" runat="server" Text='<%# Bind("Q2") %>'></asp:TextBox>
      <asp:Label ID="Label2" runat="server" Text='<%# Bind("Q2") %>' Visible="false">
      </asp:Label>
</ItemTemplate>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
        {
            //Bind your grid view
            GridView1.DataBind();
        }

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                int rowIndex = e.Row.RowIndex;
               //First fetch your textboxes and labeles 
                TextBox textBoxQ1 = GridView1.Rows[rowIndex].FindControl("Q1") as TextBox;
                TextBox textBoxQ2 = GridView1.Rows[rowIndex].FindControl("Q2") as TextBox;

                Label Label1 = GridView1.Rows[rowIndex].FindControl("Label1") as Label;
                Label Label2 = GridView1.Rows[rowIndex].FindControl("Label2") as Label;

                if (Window_name.Equals("Q2"))
                {
                    //Set 'visiblity' to 'true' for those LABEL you want to show. Sample one below
                    Label2.Visible = false;
                    //Set 'visibilty' to 'false' for those TEXT BOXES you want to hide. Sample one below
                    textBoxQ2.Visible = false;
                }
            }

Let me know in case of any queries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.