In this article I will explain with an example, how to enable or disable TextBox in GridView based on condition in ASP.Net using C# and VB.Net.
Inside the OnRowDataBound event handler, the TextBox will be referenced and disabled based on some condition in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of an ASP.Net GridView with two BoundField columns and a TemplateField column consisting of a TextBox.
The GridView has been assigned an OnRowDataBound event handler.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtCountry" Text='<%# Eval("Country") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the GridView control
Inside the Page Load event handler, the GridView is populated with a dynamic DataTable with some dummy data.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Disable TextBox in GridView based on condition inside RowDataBound event
Inside the OnRowDataBound event handler, the TextBox in the GridView Row is referenced and the TextBox is disabled based on the Customer ID value in ASP.Net.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = e.Row.Cells[0].Text;
if (customerId == "2")
{
TextBox txtCountry = (e.Row.FindControl("txtCountry") as TextBox);
txtCountry.Enabled = false;
}
}
}
VB.Net
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim customerId As String = e.Row.Cells(0).Text
If customerId = "2" Then
Dim txtCountry As TextBox = (TryCast(e.Row.FindControl("txtCountry"), TextBox))
txtCountry.Enabled = False
End If
End If
End Sub
Screenshot
Demo
Downloads