Skip to content

Instantly share code, notes, and snippets.

@jzwang-dev
Created January 29, 2021 08:52
Show Gist options
  • Save jzwang-dev/1bfbcd661831fa4a5e4cdf21af636868 to your computer and use it in GitHub Desktop.
Save jzwang-dev/1bfbcd661831fa4a5e4cdf21af636868 to your computer and use it in GitHub Desktop.
PagingAndCheckBox.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PagingAndCheckBox.aspx.vb" Inherits="PagingAndCheckBox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Grid View Paging + CheckBox</title>
<script>
function checkId(cb) {
var hidden = document.getElementById('checkedIds');
var checked_ids = hidden.value ? hidden.value.split(/\s*,\s*/) : [];
var id = cb.parentElement.attributes['data-id'].value;
var index = checked_ids.indexOf(id);
if (cb.checked) {
if (index == -1) {
checked_ids.push(id);
}
} else {
if (index != -1) {
checked_ids.splice(index, 1);
}
}
hidden.value = checked_ids.join();
}
</script>
</head>
<body>
<h5>測試GridView分頁 + CheckBox</h5>
<form id="form1" runat="server">
<asp:HiddenField ID="checkedIds" runat="server" ClientIDMode="Static" />
<asp:Button ID="btnTest" runat="server" Text="測試" />
<asp:GridView ID="gvTest" runat="server" HeaderStyle-BackColor="LightGray" AlternatingRowStyle-BackColor="LightGray" CellPadding="5"
AutoGenerateColumns="false" AllowPaging="true" PageSize="3">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbCheck" runat="server" data-id='<%#Eval("Id") %>' onclick="checkId(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment