Hi sharib42,
Refer below sample code
HTML
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat="server" class="form-select">
<asp:ListItem Text="Select Status" Value=""></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfvQuantity" ControlToValidate="txtQuantity" runat="server"
ErrorMessage="Required" ForeColor="Red" Enabled="false" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="OnSubmit" />
</div>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
CODE
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Item"), new DataColumn("Price"), new DataColumn("Quantity") });
dt.Rows.Add("Shirt");
dt.Rows.Add("Football", 299, 10);
dt.Rows.Add("Shirt", 545, 15);
dt.Rows.Add("Disc", 99, 20);
dt.Rows.Add("Watch", 200, 45);
dt.Rows.Add("Clock", 670, 97);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnSubmit(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
TextBox t_txt = (TextBox)row.FindControl("txtPrice");
DropDownList ddl1 = (DropDownList)row.FindControl("ddl1");
RequiredFieldValidator rfvQuantity = (RequiredFieldValidator)row.FindControl("rfvQuantity");
string sp_ddl1 = ddl1.SelectedValue;
if (sp_ddl1 == "Yes")
{
rfvQuantity.Enabled = true;
}
else
{
rfvQuantity.Enabled = false;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Item"), New DataColumn("Price"), New DataColumn("Quantity")})
dt.Rows.Add("Shirt")
dt.Rows.Add("Football", 299, 10)
dt.Rows.Add("Shirt", 545, 15)
dt.Rows.Add("Disc", 99, 20)
dt.Rows.Add("Watch", 200, 45)
dt.Rows.Add("Clock", 670, 97)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
For Each row As GridViewRow In GridView1.Rows
Dim t_txt As TextBox = CType(row.FindControl("txtPrice"), TextBox)
Dim ddl1 As DropDownList = CType(row.FindControl("ddl1"), DropDownList)
Dim rfvQuantity As RequiredFieldValidator = CType(row.FindControl("rfvQuantity"), RequiredFieldValidator)
Dim sp_ddl1 As String = ddl1.SelectedValue
If sp_ddl1 = "Yes" Then
rfvQuantity.Enabled = True
Else
rfvQuantity.Enabled = False
End If
Next
End Sub
Screenshot