Sunday, August 11, 2019

CheckBox inside GridView and handle its events to get records based on its Checked state

Step-1 : Create New Project

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create a table and insert data for shown in Gridview.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add Web Page.

Go to Solution Explorer > Right Click on Project under solution explorer > Add > New item > Select Web Form using Master Page under web > Enter Name > > Select Master Page>Add.
(Complete Code in Source Code)

Here is HTML Code for Gridview

  1. <asp:GridView ID="GridView1" Width="500px" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentID" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal">
  2. <Columns>
  3. <asp:TemplateField >
  4. <HeaderTemplate>
  5. &nbsp;&nbsp; <asp:CheckBox ID="chkCheckAll" runat="server" onclick="javascript:SelectAllCheckboxes(this)" />
  6. </HeaderTemplate>
  7. <ItemTemplate>
  8. &nbsp;&nbsp;<asp:CheckBox ID="chkCheck" runat="server" onclick="javascript:CheckedCheckboxes(this)" />
  9. </ItemTemplate>
  10. </asp:TemplateField>
  11. <asp:TemplateField HeaderText="Student Name">
  12. <ItemTemplate>
  13. <asp:Label ID="lblStudentName" runat="server" Text='<%#Eval("StudentName") %>'></asp:Label>
  14. </ItemTemplate>
  15. </asp:TemplateField>
  16. <asp:TemplateField HeaderText="Class">
  17. <ItemTemplate>
  18. <asp:Label ID="lblClass" runat="server" Text='<%#Eval("Class") %>'></asp:Label>
  19. </ItemTemplate>
  20. </asp:TemplateField>
  21. <asp:TemplateField HeaderText="Class">
  22. <ItemTemplate>
  23. <asp:Label ID="lblRollNo" runat="server" Text='<%#Eval("RollNo") %>'></asp:Label>
  24. </ItemTemplate>
  25. </asp:TemplateField>
  26. </Columns>
  27. <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
  28. <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
  29. <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
  30. <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
  31. <SortedAscendingCellStyle BackColor="#F7F7F7" />
  32. <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
  33. <SortedDescendingCellStyle BackColor="#E5E5E5" />
  34. <SortedDescendingHeaderStyle BackColor="#242121" />
  35. </asp:GridView>

Step-6: Add Code For Populate Data.

Write the followings code in your page load event for fetch Data from Database and show in gridview.

PAGE LOAD CODE
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. populateData();
  6. }
  7. }

And Here is the function code
  1. private void populateData()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. GridView1.DataSource = dc.StudentMasters.ToList();
  6. GridView1.DataBind();
  7. }
  8. }

Step-7: Add Javascript code for Select Gridview Record(s).

  1. <script src="Scripts/jquery-1.7.1.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. function SelectAllCheckboxes(chk) {
  4. var totalRows = $("#<%=GridView1.ClientID %> tr").length;
  5. var selected = 0;
  6. $('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
  7. if (this != chk) {
  8. this.checked = chk.checked;
  9. selected += 1;
  10. }
  11. });
  12. }
  13.  
  14. function CheckedCheckboxes(chk) {
  15. if (chk.checked) {
  16. var totalRows = $('#<%=GridView1.ClientID %> :checkbox').length;
  17. var checked = $('#<%=GridView1.ClientID %> :checkbox:checked').length
  18. if (checked == (totalRows - 1)) {
  19. $('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
  20. this.checked = true;
  21. });
  22. }
  23. else {
  24. $('#<%=GridView1.ClientID %>').find('input:checkbox:first').removeAttr('checked');
  25. }
  26. }
  27. else {
  28. $('#<%=GridView1.ClientID %>').find('input:checkbox:first').removeAttr('checked');
  29. }
  30. }
  31. </script>

Step-8: Write following code in Button Click Event to get Selected records.  

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. string Selected = "";
  4. foreach (GridViewRow gr in GridView1.Rows)
  5. {
  6. CheckBox cb = (CheckBox)gr.FindControl("chkCheck");
  7. Label lblName = (Label)gr.FindControl("lblStudentName");
  8. // You can get other value same way
  9.  
  10. if (cb != null && cb.Checked)
  11. {
  12. string StdID = GridView1.DataKeys[gr.DataItemIndex].Values["StudentID"].ToString();
  13. Selected += "Student ID : " + StdID + ", Name : " + lblName.Text.Trim() + "<br/>";
  14. }
  15. }
  16.  
  17. lblResult.Text = Selected;
  18. }

STEP-9: Run Application

Run Application and Get result in your browser

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...