Hi mahjoubi,
Please refer below sample.
MasterPage
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
HTML
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<input type="hidden" id="HeadertTxt" name="HeadertTxt" value="" />
<asp:Button ID="btnGet" Text="Get" runat="server" OnClick="OnGet" Style="display: none;" />
<script type="text/javascript">
function GetHeaderText(txt) {
alert(txt);
document.getElementById("HeadertTxt").value = txt;
document.getElementById('<%=btnGet.ClientID%>').click();
}
</script>
</asp:Content>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new DataColumn[3]{new System.Data.DataColumn("Id"),
new System.Data.DataColumn("Name"),
new System.Data.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 OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var cel = e.Row.Cells;
for (var i = 0; i < cel.Count ; i++)
{
TableCell cell = cel[i];
if (i < cel.Count - 1)
cell.Attributes.Add("onclick", "javascript: GetHeaderText('" + cel[i + 1].Text + "')");
else
cell.Attributes.Add("onclick", "javascript: GetHeaderText('" + cel[i].Text + "')");
cell.Attributes.Add("style", "cursor: pointer;");
}
}
}
protected void OnGet(Object sender, EventArgs e)
{
string rowText = (Request.Form["HeadertTxt"].ToString());
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn(2) {
New System.Data.DataColumn("Id"),
New System.Data.DataColumn("Name"),
New System.Data.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 OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cells = e.Row.Cells
For i = 0 To cells.Count - 1 Step 1
Dim cell As TableCell = cells(i)
If i < cells.Count - 1 Then
cell.Attributes.Add("onclick", "javascript: GetHeaderText('" + cells(i + 1).Text + "')")
Else
cell.Attributes.Add("onclick", "javascript: GetHeaderText('" + cells(i).Text + "')")
End If
cell.Attributes.Add("style", "cursor: pointer;")
Next
End If
End Sub
Protected Sub OnGet(sender As Object, e As EventArgs)
Dim rowText As String = (Request.Form("HeadertTxt").ToString)
End Sub
Screenshot