Asp.net-在代码隐藏处将文本框字段更改为网格视图中的标签

本文关键字:网格 视图 标签 字段 代码 net- 隐藏处 文本 Asp | 更新日期: 2023-09-27 17:58:43

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

顺便说一句,我没有使用编辑/选择网格视图模式,因为我让它批量更新网格视图(一个按钮更新所有行(

Asp.net-在代码隐藏处将文本框字段更改为网格视图中的标签

我试图在这里帮助您获得两个控件的示例和示例网格视图ID"GridView1",根据您的代码进行更改:

您可以创建标签,而不是在后面的代码中显示文本框,也可以最初创建文本框和标签,并在需要时显示它们。

此外,您可以在GridView的"RowDataBound"事件中执行此操作,而不是在Page_Load函数中执行,并在每次执行postback时绑定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;
                }
            }

如果有任何疑问,请告诉我。