Hi @HOUSSEM MAHJOUBI ,
Accroding to your description,how do you click in the gridview using button? And what's need you do to get the column header text?
I have created a demo that you could click on the column header and get the text using rowdatabound and javascript click event.Just like this:
<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false" OnRowDataBound="gv1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label runat="server" ID="lbid" Text='<%# E val("Id") >'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="num">
<ItemTemplate>
<asp:Label runat="server" ID="lbid" Text='<%# E val("num") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label runat="server" ID="lbid" Text='<%# E val("num") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
function GetHeaderText(txt) {
a lert(txt);
}
Code-behind:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (DataControlFieldCell cell in e.Row.Cells)
{
cell.Attributes.Add("onclick", "[javascript]: GetHeaderText('" + cell.Text + "')"); //You need to remove [] around the word 'javascript'
}
}
}
Result:
Edited:
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
For Each cell As DataControlFieldCell In e.Row.Cells
cell.Attributes.Add("onclick", "[javascript]:GetHeaderText('" & cell.Text & "')")
Next
End If
End Sub
<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false" OnRowDataBound="gv1_RowDataBound"></asp:GridView>
Create another gridview and show the data
Since I don't know the relationship of gridviews' data,but I suggest you could create gridview in jquery and use ajax to fill data when you click the gridview column.
Just like this:
Code-behind:
cell.Attributes.Add("onclick", "test()");
<System.Web.Services.WebMethod>
Public Shared Function test2() As String
End Function
Jquery:
$(function () {
test();
});
function test() {
$.a jax({
type: "POST",
url: "Default.aspx/test2", // call sql query in code behind
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(){
$("#gvProduct").append()// create gridview and fill data
}
failure: function (response) {
a lert(response.d);
},
});
}
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Yijing Sun