簡體   English   中英

Asp.net-將Textbox字段更改為gridview中位於后面代碼中的標簽

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM