[英]Asp.net - Changing Textbox field into label in gridview at code behind
<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>
我目前有一个带有字段作为文本框的页面,我想根据后面代码中的条件更改其中一些标签。
例如,如果Window_name ='Q2'->使Q2 Q3 Q4文本框和Q1标签(如果是Window_name ='Q3'则使Q3和Q4文本框但Q1和Q2标签)
顺便说一句,我没有使用编辑/选择gridview模式,因为我将其批量更新gridview(一个按钮来更新所有行)
我正在尝试为您提供两个控件的示例以及示例网格视图ID“ GridView1”,请根据您的代码对其进行更改:
您可以创建标签而不是在CODE BEHIND中显示文本框,也可以先创建文本框和标签,然后在需要时显示它们。
另外,也可以在GridView的'RowDataBound'事件中执行此操作,而不是在Page_Load函数中执行此操作,并在每次回发完成后绑定GridView。
ASPX代码:
<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>
背后的代码:
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;
}
}
如有任何疑问,请通知我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.