Hi ahmadsubuhanl...,
Refer below example.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Name">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Get" runat="server" OnClick="GetDetails" />
Namespaces
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("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
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();
}
}
protected void GetDetails(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string name = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
string country = (row.Cells[2].FindControl("txtCountry") as TextBox).Text;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert" + row.RowIndex,
"alert('" + string.Format("Name:{0}\\nCountry:{1}", name, country) + "');", true);
}
}
}
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("Id"), New DataColumn("Name"), New DataColumn("Country")})
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
Protected Sub GetDetails(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim name As String = GridView1.DataKeys(row.RowIndex).Values(0).ToString()
Dim country As String = (TryCast(row.Cells(2).FindControl("txtCountry"), TextBox)).Text
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert" & row.RowIndex, "alert('" & String.Format("Name:{0}\nCountry:{1}", name, country) & "');", True)
End If
Next
End Sub
Screenshot