jQuery click gridview row in master page
I have been trying to figure out how to allow the user to click a row in a gridview in a master page. Ultimately I want the user to be redirected to another page with a values from the gridview (hiddenfield column) to be the querystring value of the p开发者_如何学Goage redirected to.
A this point I can only get to the gridview itself. I cannot get the row or the cell from the gridview.
<script type="text/javascript">
$(document).ready(function () {
$('[id$=LeagueGV]').click(function() { alert('gridview clicked'); });
//window.location = "/League_Admin.aspx?LID=" + $(this).find('LeagueHidden').value();
//});
});
</script>
<asp:GridView runat="server" ID="LeagueGV" DataKeyNames="LeagueID" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="LeagueHidden" Value='<%# Eval("LeagueID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="LeagueIDLabel" Text='<%#Eval("LeagueID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LeagueName" HeaderText="League Name" />
</Columns>
</asp:GridView>
There's a couple problems with what you have. First off, your GridView
's ID won't be rendered as "LeagueGV", nor will the hidden field be "LeagueHidden". It will be mangled to be part of a chain of its parent NamingContainer
's IDs.
Secondly, jQuery syntax for using IDs can be as simple as $('#yourId')
. The syntax you're using is old.
Thirdly, I wouldn't recommend adding a click event to a table
or a tr
. It's not an expected behavior by many users and could be confusing.
Unless you're hoping for unobtrusive Javascript, my first suggestion would be to have an HyperLinkField
instead of a TemplateField
, then you could override the OnDataBound
event for the GridView
and add an onclick
attribute to the link when it renders that includes the LeagueID.
精彩评论