Reputation: 77
<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)
Upvotes: 0
Views: 5054
Reputation: 417
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.
Upvotes: 2