<p>I'm presenting information from a DataTable on my page and would like to add some sorting functionality which goes a bit beyond a straight forward column sort. As such I have been trying to place LinkButtons in the HeaderItems of my GridView which post-back to functions that change session information before reloading the page.</p>

<p>Clicking my links <em>DOES</em> cause a post-back but they don't seem to generate any <em>OnClick</em> events as my <em>OnClick</em> functions don't get executed. I have <code>AutoEventWireup</code> set to true and if I move the links out of the GridView they work fine.</p>

<p>I've got around the problem by creating regular anchors, appending queries to their <strong>hrefs</strong> and checking for them at page load but I'd prefer C# to be doing the grunt work. Any ideas?</p>

<p><strong>Update:</strong> To clarify the IDs of the controls match their <em>OnClick</em> function names.</p>

[{'answer_id': 45477, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 0, 'selected': False, 'text': '<p>Two things to keep in mind when using events on dynamically generated controls in ASP.Net:</p>\n\n<ul>\n<li>Firstly, the controls should ideally be created in the Page.Init event handler. This is to ensure that the controls have already been created before the event handling code is ran.</li>\n<li>Secondly, you must assign the same value to the controls ID property, so that the event handler code knows that that was the control that should handle the event.</li>\n</ul>\n'}, {'answer_id': 45513, 'author': 'NakedBrunch', 'author_id': 3742, 'author_profile': 'https://Stackoverflow.com/users/3742', 'pm_score': 2, 'selected': False, 'text': '<p>You\'re on the right track but try working with the Command Name/Argument of the LinkButton.  Try something like this:</p>\n\n<p>In the HeaderTemplate of the the TemplateField, add a LinkButton and set the CommandName and CommandArgument</p>\n\n<pre><code>&lt;HeaderTemplate&gt; \n    &lt;asp:LinkButton ID="LinkButton1" runat="server" CommandName="sort" CommandArgument="Products" Text="&lt;%# Bind(\'ProductName")\' /&gt;\n&lt;/HeaderTemplate&gt;\n</code></pre>\n\n<p>Next, set the RowCommand event of the GridView</p>\n\n<pre><code>protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)\n{\n    if (e.CommandName == "sort")\n    {\n        //Now sort by e.CommandArgument\n\n    }\n}\n</code></pre>\n\n<p>This way, you have a lot of control of your LinkButtons and you don\'t need to do much work to keep track of them.</p>\n'}, {'answer_id': 45599, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 0, 'selected': False, 'text': '<p>You can specify the method to call when the link is clicked.</p>\n\n<pre><code>&lt;HeaderTemplate&gt;\n    &lt;asp:LinkButton\n        ID="lnkHdr1"\n        Text="Hdr1"\n        OnCommand="lnkHdr1_OnCommand"\n        CommandArgument="Hdr1"\n        runat="server"&gt;&lt;/asp:LinkButton&gt;\n&lt;/HeaderTemplate&gt;\n</code></pre>\n\n<p>The code-behind:</p>\n\n<pre><code>protected void lnkHdr1_OnCommand(object sender, CommandEventArgs e)\n{\n    // e.CommandArgument\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/45475', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4431/']