Now, the rendered HTML table will
include id property to every TR tag. Refer below,
<table cellspacing="0" rules="all"
border="1" id="gvUsers" style="border-collapse:collapse;">
<tr
style="color:#FFFFCC;background-color:#F06300;font-weight:bold;">
<th
scope="col">First Name</th><th scope="col">Last
Name</th><th scope="col">Email</th>
</tr><tr
id="1" style="color:#330099;background-color:White;">
<td>Satheesh</td><td>babu</td><td>satheesh@gmail.com</td>
</tr><tr
id="2" style="color:#330099;background-color:White;">
<td>vadivel</td><td>m</td><td>vadi@gmail.com</td>
</tr><tr
id="3" style="color:#330099;background-color:White;">
<td>John</td><td>m</td><td>John@gmail.com</td>
</tr><tr
id="4" style="color:#330099;background-color:White;">
<td>Peter</td><td>m</td><td>Peter@gmail.com</td>
</tr><tr
id="5" style="color:#330099;background-color:White;">
<td>Kavi</td><td>m</td><td>Kavi@gmail.com</td>
</tr><tr
id="6" style="color:#330099;background-color:White;">
<td>Freddy</td><td>m</td><td>Freddy@gmail.com</td>
</tr><tr
id="7" style="color:#330099;background-color:White;">
<td>martin</td><td>m</td><td>martin@gmail.com</td>
</tr>
</table>
6. Next, we will integrate the jquery library and make the GridView
row(TR tag) selectable. Refer the FAQs above to integrate the jQuery
library.
7. To make every row selectable, we can add a click event to every TR
except header. Refer the below code,
<script
src="_scripts/jquery-1.4.1.min.js"
type="text/javascript"></script>
<script
type="text/javascript">
$(function() {
$('#<%=gvUsers.ClientID%>
tr[id]').click(function() {
$('#<%=gvUsers.ClientID%> tr[id]').css({ "background-color": "White",
"color": "Black" });
$(this).css({
"background-color": "Black", "color": "White" });
});
$('#<%=gvUsers.ClientID%>
tr[id]').mouseover(function() {
$(this).css({ cursor: "hand",
cursor: "pointer" });
});
});
</script>
The selector expression tr[id] will
ensure the click event is added to only datarows since we have added id
attribute to only data rows from code behind(step 5). Execute the page and you
can see the selectable gridview in action.
8. Now, we need to send the selected rows identity (primary key) to the
codebehind so that it can be selected to complete its action. To do this, we
will add a hidden field(hdnEmailID) to the aspx page and fill it with the
identity of the selected row from jquery script. Update the above jquery code to
set the hidden field with the identity key value. Refer the bolded code
below,
<script
type="text/javascript">
$(function() {
$('#<%=gvUsers.ClientID%>
tr[id]').click(function() {
$('#<%=gvUsers.ClientID%> tr[id]').css({ "background-color": "White",
"color": "Black" });
$(this).css({
"background-color": "Black", "color": "White" });
$('#hdnEmailID').val($(this).attr("id"));
});
$('#<%=gvUsers.ClientID%>
tr[id]').mouseover(function() {
$(this).css({ cursor: "hand",
cursor: "pointer" });
});
});
</script>
<INPUT id="hdnEmailID" type="hidden" value="0" runat="server" >
You can access the hidden field value
in codebehind complete the action required in the codebehind file. Refer the
button click events below,
protected void btnSelect_Click(object
sender, EventArgs e)
{
Response.Write(hdnEmailID.Value);
//Do your action
}
protected void btnDelete_Click(object
sender, EventArgs e)
{
Response.Write(hdnEmailID.Value);
//Do your action
hdnEmailID.Value = "0";
}
Retaining Selection after a Select
Action
When we do a select action i.e.
clicking select button, it will be good if we retain the selected row after the
postback to know the selected row. The below code does that. Refer the bolded
code.
<script
src="_scripts/jquery-1.4.1.min.js"
type="text/javascript"></script>
<script
type="text/javascript">
$(function() {
var RowID =
$('#hdnEmailID').val();
if (RowID != "0")
{
$('#<%=gvUsers.ClientID%> tr[id=' + RowID + ']').css({ "background-color":
"Black", "color": "White" });
}
$('#<%=gvUsers.ClientID%>
tr[id]').click(function() {
$('#<%=gvUsers.ClientID%> tr[id]').css({ "background-color": "White",
"color": "Black" });
$(this).css({
"background-color": "Black", "color": "White" });
$('#hdnEmailID').val($(this).attr("id"));
});
$('#<%=gvUsers.ClientID%>
tr[id]').mouseover(function() {
$(this).css({ cursor: "hand",
cursor: "pointer" });
});
});
</script>
Note:
Set the hidden field to 0 in delete
action after deleting the row.
|