Hi lingers,
Set the TextBox AutoPostBack property to true.
Then OnTextChanged event find the GridViewRow using NamingContainer property.
Then using the reference of GridViewRow find the Cell value.
Refer below modified code.
HTML
<asp:GridView ID="avDetails" runat="server" AutoGenerateColumns="False" Font-Names="Century Gothic"
Font-Size="8px" DataKeyNames="pid" Width="100%" Style="margin-bottom: 0px"
CssClass="grid" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="no" HeaderText=" base NO" />
<asp:BoundField DataField="Pid" HeaderText="PROD CODE" />
<asp:BoundField DataField="posino" HeaderText="POSI NO" />
<asp:BoundField DataField="description" HeaderText="DESCRIPTION" />
<asp:BoundField DataField="colour" HeaderText="COLOUR" />
<asp:TemplateField HeaderText="Quantity Produced">
<ItemTemplate>
<asp:TextBox ID="txtcomment1" OnTextChanged="TextBox1_TextChanged"
AutoPostBack="true" runat="server" Width="30"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="deliverydate" HeaderText="RECEIVED" />
<asp:BoundField DataField="supplier" HeaderText="SUPPLIER" />
<asp:BoundField DataField="totalrev" HeaderText="TOTAL REV" />
<asp:BoundField DataField="screen" HeaderText="SCR/ANG" />
<asp:BoundField DataField="depth" HeaderText="DEPTH" />
<asp:BoundField DataField="circumference" HeaderText="CIRC" />
<asp:BoundField DataField="" HeaderText="STATUS" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails1();
}
}
protected void BindUserDetails1()
{
string constr1 = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(constr1))
{
using (SqlDataAdapter sda1 = new SqlDataAdapter("SELECT distinct j.pid, j.Description, s.no,s.totalrev, js.id,js.posino,js.Colour,js.screen,js.depth,js.circumference ,o.deliverydate,o.supplier FROM Stock s INNER JOIN Job j ON j.Id = s.pid inner join orders o on o.orderid =s.orderid INNER JOIN (SELECT id,posino,colour,screen,depth,circumference FROM job_cylinder WHERE posino IN (SELECT posino FROM job_cylinder WHERE pid = '568') AND PosiNo <> '') js ON js.id = s.typeid ", con1))
{
using (DataTable dt1 = new DataTable())
{
sda1.Fill(dt1);
avDetails.DataSource = dt1;
avDetails.DataSource = dt1;
avDetails.DataBind();
}
}
}
if (avDetails.Rows.Count > 0)
{
avDetails.UseAccessibleHeader = true;
avDetails.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
string name = row.Cells[0].Text;
ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('" + name + "')", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindUserDetails1()
End If
End Sub
Protected Sub BindUserDetails1()
Dim constr1 As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con1 As SqlConnection = New SqlConnection(constr1)
Using sda1 As SqlDataAdapter = New SqlDataAdapter("SELECT distinct j.pid, j.Description, s.no,s.totalrev, js.id,js.posino,js.Colour,js.screen,js.depth,js.circumference ,o.deliverydate,o.supplier FROM Stock s INNER JOIN Job j ON j.Id = s.pid inner join orders o on o.orderid =s.orderid INNER JOIN (SELECT id,posino,colour,screen,depth,circumference FROM job_cylinder WHERE posino IN (SELECT posino FROM job_cylinder WHERE pid = '568') AND PosiNo <> '') js ON js.id = s.typeid ", con1)
Using dt1 As DataTable = New DataTable()
sda1.Fill(dt1)
avDetails.DataSource = dt1
avDetails.DataSource = dt1
avDetails.DataBind()
End Using
End Using
End Using
If avDetails.Rows.Count > 0 Then
avDetails.UseAccessibleHeader = True
avDetails.HeaderRow.TableSection = TableRowSection.TableHeader
End If
End Sub
Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender, TextBox).NamingContainer, GridViewRow)
Dim name As String = row.Cells(0).Text
ClientScript.RegisterStartupScript(Me.GetType(), "message", "alert('" & name & "')", True)
End Sub