0

I had a grid view populated from dataset and I have to redirect another page when user clicks on gridview header. How can I get the gridview header's text that is clicked by the user . I tried some code here...

protected void gv2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.Header)
        {            
            e.Row.Attributes.Add("onclick", "location='/SampleProgram/AnotherPage.aspx?empid=" + e.Row .Cells[0].Text+ "'");//this will give me first column header's text.

        }
    }

Thx a lot for your help and interest...

nnnn
  • 1,041
  • 3
  • 18
  • 35
  • and what exactly is not working? when the HTML is rendered, does the header have an `onclick` attribute? – RPM1984 Nov 15 '10 at 05:18
  • Why would you want to redirect to another page when a user clicks on the GridView header? – Jamie Nov 17 '10 at 10:27

2 Answers2

1

Here is my answer..

 foreach (DataControlFieldCell cell in e.Row.Cells)
                    {
                        cell.Attributes.Add("id", _i.ToString());
                        cell.Attributes.Add("onClick", "location='/SampleProgram/AnotherPage.aspx?empid="+e.Row.Cells[_i].Text+"'");  
                        _i++;
                    }

使ってみてください。 :)

BayoteYC
  • 28
  • 1
  • 6
1

Here is a solution from the jQuery:

$("table").delegate("th", "click", function() {
    var i = $(this).index();
    alert("th:" + $(this).closest("table").find("th").eq(i).text());
});

The above code will get you the Table header in the Gridview.
You can try the demo here: http://jsfiddle.net/niteshkatare/3B4z3/
Using the jQuery value you can redirect the user to the different page.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Nitish Katare
  • 1,147
  • 2
  • 12
  • 22