简体   繁体   中英

how to create enable disable textbox inside gridview

if (e.Row.DataItem != null)
{
    System.Web.UI.WebControls.TextBox txtGLCode = 
       e.Row.FindControl("txtGLCode") as System.Web.UI.WebControls.TextBox;

    string Enable = e.Row.Cells[4].Text;

    if (Enable == "True")
    {
        txtGLCode.Enabled = false;
        txtGLCode.BackColor = Color.White;
    }
}

If it is an ASP web page you could do it like this

<asp:gridview id="GridView" runat="server" onrowdatabound="GridView_RowDataBound" xmlns:asp="#unknown">
<columns>
  <asp:templatefield headertext="TextBox">
   <itemtemplate>
    <asp:textbox id="TextBox1" runat="server" Text='<%#Bind("ColumnName")%>' />
   </itemtemplate>
  </asp:templatefield>  
</columns>

Where column name is the column from the datatable that you are binding to you GridView. You can the reference the TextBox control using the id from code behind. To hide it you can do it in two ways, one is from the markup which is done like below

<asp:gridview id="GridView" runat="server" onrowdatabound="GridView_RowDataBound" xmlns:asp="#unknown">
<columns>
  <asp:templatefield headertext="TextBox">
   <itemtemplate>
    <asp:textbox id="TextBox1" runat="server" Text='<%#Bind("ColumnName")%>' Visible='<%# iif(Expression, true, false) %>'/>
   </itemtemplate>
  </asp:templatefield>  
</columns>

Expression: Will be what you want to the textbox to be shown when it true. I hope this gives you an insight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM