Hi ihechi,
Please refer below sample code.
HTML
<div>
<asp:GridView ID="grvspouse" RowStyle-Wrap="false" GridLines="None" CssClass="responsiveTable1"
runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="CustomerId" Visible="false" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Name">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:TextBox ID="txtName" Text='<%# Eval("Name") %>' placeholder="Name...(e.g, Jane Doe)"
runat="server" class="form-control" AutoPostBack="true" OnTextChanged="txtspousename_TextChanged"></asp:TextBox><br />
<asp:CheckBox ID="spouseDetails" ClientIDMode="Static" runat="server" Checked="false"
AutoPostBack="true" OnCheckedChanged="SpouseCheckChanged" /><span style="color: #ff0000">*Check
this box if N/A</span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtAddress" Text='<%# Eval("Country") %>' placeholder="Address..."
runat="server" class="form-control" AutoPostBack="true" OnTextChanged="txtspousename_TextChanged"></asp:TextBox><br />
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
grvspouse.DataSource = dt;
grvspouse.DataBind();
}
}
}
}
}
protected void txtspousename_TextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
TextBox spousename = row.FindControl("txtName") as TextBox;
TextBox spouseaddress = row.FindControl("txtAddress") as TextBox;
CheckBox spouseSelect = row.FindControl("spouseDetails") as CheckBox;
if (spousename.Text.Length > 0 && spouseaddress.Text.Length > 0)
spouseSelect.Enabled = false;
else
spouseSelect.Enabled = true;
}
protected void SpouseCheckChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as CheckBox).NamingContainer as GridViewRow;
TextBox spousename = row.FindControl("txtName") as TextBox;
TextBox spouseaddress = row.FindControl("txtAddress") as TextBox;
CheckBox spouseSelect = row.FindControl("spouseDetails") as CheckBox;
if (!spouseSelect.Checked)
{
spousename.Enabled = true;
spouseaddress.Enabled = true;
}
else
{
spousename.Enabled = false;
spouseaddress.Enabled = false;
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox spousename = e.Row.FindControl("txtName") as TextBox;
TextBox spouseaddress = e.Row.FindControl("txtAddress") as TextBox;
CheckBox spouseSelect = e.Row.FindControl("spouseDetails") as CheckBox;
if (!string.IsNullOrEmpty(spousename.Text.Trim()) && !string.IsNullOrEmpty(spouseaddress.Text.Trim()))
{
spouseSelect.Enabled = false;
}
else
{
spouseSelect.Enabled = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Customers"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
grvspouse.DataSource = dt
grvspouse.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub txtspousename_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim spousename As TextBox = TryCast(row.FindControl("txtName"), TextBox)
Dim spouseaddress As TextBox = TryCast(row.FindControl("txtAddress"), TextBox)
Dim spouseSelect As CheckBox = TryCast(row.FindControl("spouseDetails"), CheckBox)
If spousename.Text.Length > 0 AndAlso spouseaddress.Text.Length > 0 Then
spouseSelect.Enabled = False
Else
spouseSelect.Enabled = True
End If
End Sub
Protected Sub SpouseCheckChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, CheckBox)).NamingContainer, GridViewRow)
Dim spousename As TextBox = TryCast(row.FindControl("txtName"), TextBox)
Dim spouseaddress As TextBox = TryCast(row.FindControl("txtAddress"), TextBox)
Dim spouseSelect As CheckBox = TryCast(row.FindControl("spouseDetails"), CheckBox)
If Not spouseSelect.Checked Then
spousename.Enabled = True
spouseaddress.Enabled = True
Else
spousename.Enabled = False
spouseaddress.Enabled = False
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim spousename As TextBox = TryCast(e.Row.FindControl("txtName"), TextBox)
Dim spouseaddress As TextBox = TryCast(e.Row.FindControl("txtAddress"), TextBox)
Dim spouseSelect As CheckBox = TryCast(e.Row.FindControl("spouseDetails"), CheckBox)
If Not String.IsNullOrEmpty(spousename.Text.Trim()) AndAlso Not String.IsNullOrEmpty(spouseaddress.Text.Trim()) Then
spouseSelect.Enabled = False
Else
spouseSelect.Enabled = True
End If
End If
End Sub
Screenshot