Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > ASP.NET GridView and Checkbox Select All Row Using jQuery




Clound SSD Virtual Server

ASP.NET GridView and Checkbox Select All Row Using jQuery

 
  ASP.NET GridView and Checkbox Select All Row Using jQuery ทำปุ่ม Check All /Un Check All เลือกทั้งหมด และไม่เลือกทั้งหมด บทความ ASP.NET การใช้งาน jQuery เพื่อเลือกแถว Checkbox Check All ใน GridView เลือกแถวทั้งหมด หรือไม่เลือกทั้งหมด โดยใช้ jQuery ในการอ้างถึง Selectors Checkbox ใน GridView

ASP.NET GridView jQuery and Checkbox Select All

ซึ่งตามที่เราเข้าใจแล้วว่าใน GridView นั้นจะไม่สามารถทำการเรียก element ที่อยู่ภายใน GridView ได้โดยตรง เพราะใน GridView จะมีการ Generate ClientID ให้กับ element ใหม่ เช่น <input type="button" id="btnCheckAll" value="Check All" /> ที่อยู่ใน GridView เราจะไม่สามารถเรียก $("#btnCheckAll") ได้โดยตรง เพราะ GridView อาจจะทำการ Genrate id ใหม่ ที่ขึ้นด้วยด้วย ชื่อกริดวิว_ลำดับ_ไอดี เช่น myGridView_ctl01_CheckAll

	<table cellspacing="0" rules="all" border="1" id="myGridView" style="border-collapse:collapse;">

		<tr>

			<th scope="col">

                    <input id="myGridView_ctl01_CheckAll" type="checkbox" name="myGridView$ctl01$CheckAll" />

                </th><th scope="col">CustomerID</th><th scope="col">Name</th><th scope="col">Email</th><th scope="col">CountryCode</th><th scope="col">Budget</th><th scope="col">Used</th>

		</tr><tr>

			<td>

                    <input id="myGridView_ctl02_CheckID" type="checkbox" name="myGridView$ctl02$CheckID" />

                </td><td>

			        <span id="myGridView_ctl02_lblCustomerID">C001</span>

		        </td><td>

			        <span id="myGridView_ctl02_lblName">Win Weerachai</span>

		        </td><td>

			        <span id="myGridView_ctl02_lblEmail">[email protected]</span>

		        </td><td>

			        <span id="myGridView_ctl02_lblCountryCode">TH</span>

		        </td><td>

			        <span id="myGridView_ctl02_lblBudget">1,000,000.00</span>

		        </td><td>

			        <span id="myGridView_ctl02_lblUsed">600,000.00</span>

		        </td>

		</tr><tr>

	</table>


เมื่อ View Source ในหน้า HTML ของ Web Browser จะเห็นว่า GridView มีการ Generate ClientID ใหม่ให้กับ Control


แต่ปัญหานี้ไม่ใช่เรื่องใหญ่สำหรับ jQuery เพราะใน jQuery ได้ออกแบบการอ้างถึง element ที่สามารถอ้างถึง type , name , attribute หรืออื่น ๆ ในรูปแบบต่าง ๆ ได้อีกมากมาย เช่น

$('#<%=myGridView.ClientID%> input[id*="CheckAll"]:checkbox').

คือ การอ้างถึง table ที่มี id="myGridView" และ input ที่มี id คำว่า CheckAll อยู่ในชื่อของ id ไม่ว่าตำแหน่งใดก็ตาม

สำหรับการใช้งาน Selectors ของ jQuery สามารถอ่านได้ที่นี่
Go to : jQuery Selectors : jQuery Selectors and Element
Go to : jQuery Selectors : jQuery and Selectors การเรียกใช้งาน Selectors ของ jQuery ในการอ้างถึง element ต่าง ๆ









Code ทั้งหมด

สร้าง Project ด้วย ASP.NET Web Site และสร้าง หน้า Web Page ดังรูป

ASP.NET GridView jQuery and Checkbox Select All


Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ThaiCreate.Com Tutorials</title>
     <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
     <script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
</head>
<body>
    <script type="text/javascript">
    
        /*** for CheckAll ***/
        $(document).ready(function() {
            $('#<%=myGridView.ClientID%> input[id*="CheckAll"]:checkbox').click(
                 function() {
                    $('#<%=myGridView.ClientID%> input[id*="CheckID"]:checkbox').attr('checked', $('#<%=myGridView.ClientID%> input[id*="CheckAll"]:checkbox').is(':checked'));
                 }
             );
        });

        /*** for btnCheckAll ***/
        $(document).ready(function() {
            $('#btnCheckAll').click(
             function() {
                 $('#<%=myGridView.ClientID%> input[id*="CheckAll"]:checkbox').attr('checked', true);
                 $('#<%=myGridView.ClientID%> input[id*="CheckID"]:checkbox').attr('checked', true);
             }
            );
         });

         /*** for btnUncheckAll ***/
         $(document).ready(function() {
            $('#btnUncheckAll').click(
             function() {
                $('#<%=myGridView.ClientID%> input[id*="CheckAll"]:checkbox').attr('checked', false);
                $('#<%=myGridView.ClientID%> input[id*="CheckID"]:checkbox').attr('checked', false);
             }
            );
         });        
        
     </script>
     
    <form id="form1" runat="server">
    
       <input type="button" id="btnCheckAll" value="Check All" /> 
       <input type="button" id="btnUncheckAll" value="Uncheck All" />
    
        <asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False">
	        <Columns>
	        <asp:TemplateField>
		        <HeaderTemplate>
                    <asp:CheckBox ID="CheckAll" runat="server" />
                </HeaderTemplate>
		        <ItemTemplate>
                    <asp:CheckBox ID="CheckID" runat="server" />
                </ItemTemplate>
	        </asp:TemplateField>
	        
	        <asp:TemplateField HeaderText="CustomerID">
		        <ItemTemplate>
			        <asp:Label id="lblCustomerID" runat="server"></asp:Label>
		        </ItemTemplate>
	        </asp:TemplateField>

	        <asp:TemplateField HeaderText="Name">
		        <ItemTemplate>
			        <asp:Label id="lblName" runat="server"></asp:Label>
		        </ItemTemplate>
	        </asp:TemplateField>

	        <asp:TemplateField HeaderText="Email">
		        <ItemTemplate>
			        <asp:Label id="lblEmail" runat="server"></asp:Label>
		        </ItemTemplate>
	        </asp:TemplateField>

	        <asp:TemplateField HeaderText="CountryCode">
		        <ItemTemplate>
			        <asp:Label id="lblCountryCode" runat="server"></asp:Label>
		        </ItemTemplate>
	        </asp:TemplateField>

	        <asp:TemplateField HeaderText="Budget">
		        <ItemTemplate>
			        <asp:Label id="lblBudget" runat="server"></asp:Label>
		        </ItemTemplate>
	        </asp:TemplateField>

	        <asp:TemplateField HeaderText="Used">
		        <ItemTemplate>
			        <asp:Label id="lblUsed" runat="server"></asp:Label>
		        </ItemTemplate>
	        </asp:TemplateField>

	        </Columns>
        </asp:GridView>
    <asp:Button ID="Button1" runat="server" Text="Button" /><br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>



Default.aspx.vb
Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim objConn As OleDbConnection
    Dim objCmd As OleDbCommand

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strConnString As String
        strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
        Server.MapPath("~/App_Data/mydatabase.mdb") & ";"
        objConn = New OleDbConnection(strConnString)
        objConn.Open()

        If Not Page.IsPostBack() Then
            BindData()
        End If
    End Sub

    Protected Sub BindData()
        Dim strSQL As String
        strSQL = "SELECT * FROM customer"

        Dim dtReader As OleDbDataReader
        objCmd = New OleDbCommand(strSQL, objConn)
        dtReader = objCmd.ExecuteReader()

        '*** BindData to GridView ***'
        myGridView.DataSource = dtReader
        myGridView.DataBind()

        dtReader.Close()
        dtReader = Nothing

    End Sub

    Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles myGridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            '*** CustomerID ***'
            Dim lblCustomerID As Label = CType(e.Row.FindControl("lblCustomerID"), Label)
            If Not IsNothing(lblCustomerID) Then
                lblCustomerID.Text = e.Row.DataItem("CustomerID")
            End If

            '*** Name ***'
            Dim lblName As Label = CType(e.Row.FindControl("lblName"), Label)
            If Not IsNothing(lblName) Then
                lblName.Text = e.Row.DataItem("Name")
            End If

            '*** Email ***'
            Dim lblEmail As Label = CType(e.Row.FindControl("lblEmail"), Label)
            If Not IsNothing(lblEmail) Then
                lblEmail.Text = e.Row.DataItem("Email")
            End If

            '*** CountryCode ***'
            Dim lblCountryCode As Label = CType(e.Row.FindControl("lblCountryCode"), Label)
            If Not IsNothing(lblCountryCode) Then
                lblCountryCode.Text = e.Row.DataItem("CountryCode")
            End If

            '*** Budget ***'
            Dim lblBudget As Label = CType(e.Row.FindControl("lblBudget"), Label)
            If Not IsNothing(lblBudget) Then
                lblBudget.Text = FormatNumber(e.Row.DataItem("Budget"), 2)
            End If

            '*** Used ***'
            Dim lblUsed As Label = CType(e.Row.FindControl("lblUsed"), Label)
            If Not IsNothing(lblUsed) Then
                lblUsed.Text = FormatNumber(e.Row.DataItem("Used"), 2)
            End If
        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        objConn.Close()
        objConn = Nothing
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CheckID As CheckBox
        Dim lblCustomerID As Label
        Dim i As Integer
        Label1.Text = ""
        For i = 0 To myGridView.Rows.Count - 1
            CheckID = myGridView.Rows(i).FindControl("CheckID")
            lblCustomerID = myGridView.Rows(i).FindControl("lblCustomerID")
            If CheckID.Checked = True Then
                '*** Have lblID.Text ***'
                Me.Label1.Text = Me.Label1.Text & "<br>" & lblCustomerID.Text
            End If
        Next
    End Sub
End Class



Screeshot

ASP.NET GridView jQuery and Checkbox Select All

สามารถดาวน์โหลด Code ได้จากข้างล่างนี้

สำหรับตัวอย่างนี้จะเป็นภาษา VB.NET แต่ถ้าหากต้องการใช้ภาษา C# สามารถอ่านได้ที่บทความนี้
Go to : (C#) ASP.NET GridView Control - FindControl








บทความอื่น ๆ ของ jQuery และ ASP.NET
Go to : Basic ASP.NET jQuery Framework (พื้นฐานง่าย ๆ ด้วย ASP.NET กับ jQuery)
Go to : ASP.NET GridView Control - FindControl
Go to : ASP.NET Microsoft Access Multiple Checkbox Delete Record


บทความ jQuery พื้นฐาน
Go to : jQuery Tutorial : สอน jQuery เขียน jQuery กับ JavaScript เรียน jQuery ในประเทศไทย
Go to : jQuery : Whats a jQuery , jQuery คืออะไร ??
Go to : jQuery : How to use , จะเขียน jQuery จะต้องทำอย่างไร
Go to : jQuery Syntax : jQuery Basic Syntax
Go to : jQuery Selectors : jQuery Selectors and Element


       
Bookmark.   
       

 

  By : TC Admin
  Score Rating : -
  Create Date : 2011-10-26 15:13:56
  Download : Download  ASP.NET GridView and Checkbox Select All Row Using jQuery (1.00 MB)
     

Clound SSD Virtual Server
-->
Related Links
.NET Windows Form กับการส่งอีเมล์ (Send Mail) แบบง่าย ๆ ด้วย System.Net.Mail (VB.NET , C#)
.NET Windows Form กับการส่งอีเมล์ (Send Mail) แบบง่าย ๆ ด้วย System.Net.Mail (VB.NET , C#)
ง่าย ๆ กับ .NET Framework การเขียน Windows Form ส่งอีเมล์ แบบง่าย ๆ ด้วย Namespace ของ System.Net.Mail
Rating :
Update :
2017-03-17 22:15:41 View : 18,228
สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step
สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step
บทความการสร้างรายงานแบบง่าย ๆ บน Crystal Report ซึ่งเป็น Report Tools ที่ได้รับความนิยมมากที่สุด ทำงานร่วมกับ VB.NET และ C#
Rating :
Update :
2017-03-24 21:25:06 View : 113,960
.NET Framework รวบรวมเทคนิคการพัฒนาโปรแกรมด้วย .NET ทั้ง VB.NET และ C#
.NET Framework รวบรวมเทคนิคการพัฒนาโปรแกรมด้วย .NET ทั้ง VB.NET และ C#
หัวข้อนี้ผมได้รวบรมเทคนิคต่าง ๆ ที่เกี่ยวข้องกับการพัฒนาโปรแกรมด้วย .NET Framework ครบคลุมทั้ง Framework 1,2,3 และภาษา VB.NET,C#
Rating :
Update :
2017-03-24 21:23:16 View : 15,253
Install .NET Framework 2.0 & Visual Studio 2005
Install .NET Framework 2.0 & Visual Studio 2005
Install .NET Framework 2.0 & Visual Studio 2005 ติดตั้งและใช้งาน .NET Framework 2.0 และ VS 2005
Rating :
Update :
2009-01-11 09:44:53 View : 8,875
DataGridView , ComboBox , ListBox : Basic in (.NET) Windows Forms Application
DataGridView , ComboBox , ListBox : Basic in (.NET) Windows Forms Application
Basic พื้นฐานการสร้าง DataGridView , ComboBox , ListBox บน Windows Form ด้วย Wizard ของ Visual Studio
Rating :
Update :
2017-03-24 21:18:46 View : 30,702
Generating Word Document in .NET Framework
Generating Word Document in .NET Framework
สร้างไฟล์เอกสาร Word Document บน Windows Application และ Console Application ด้วย Framework
Rating :
Update :
2017-03-24 21:20:55 View : 6,537
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่

Inline