473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

checkbox control in asp.net

ramanan ram
i am using asp.net with oracle,now i am selecting the record [b]for example: table name emp it contains 50 records now display whole records in asp executing page each records end i am added checkbox control ,if i am choosing any one of the checkbox then press submit button that corresponding record want to delete based on the autoid.
this id field i am maintaining [ /B]
plz give solution

thank u
RAMANAN RAM
Mar 12 '12 #1

✓ answered by Frinavale

In the Button Click event, loop through all of the list items in the CheckBoxList and delete the corresponding record if it is selected.

Here's a working example.

The ASP markup code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title>Exploring</title>
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.     <div>
  11.         <asp:CheckBoxList ID="theCheckboxList" runat="server">
  12.         </asp:CheckBoxList>
  13.         <asp:Button ID="DeleteSelectedRecord" runat="server" OnClick="DeleteSelectedRecord_Click" Text="Delete Checked Items"/>
  14.          <asp:Button ID="ResetDataSource" runat="server" OnClick="ResetDataSource_Click" Text="Reset Data Source"/>
  15.     </div>
  16.     </form>
  17. </body>
  18. </html>
The C# code behind:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class WebForm1 : System.Web.UI.Page
  11.     {
  12.         /// <summary>
  13.         /// This property is used to get and set the source for the grid.
  14.         /// If there is not already a source stored in session, it creates one.
  15.         /// </summary>
  16.         private System.Data.DataTable Source
  17.         {
  18.             get
  19.             {
  20.                 source = (System.Data.DataTable)Session["source"];
  21.                 if (source == null)
  22.                 {
  23.                     source = new System.Data.DataTable();
  24.                     source.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
  25.                     source.Columns.Add(new System.Data.DataColumn("Name"));
  26.                     for (int i = 0; i < 10; i++)
  27.                     {
  28.                         System.Data.DataRow dr = source.NewRow();
  29.                         dr["ID"] = i;
  30.                         dr["Name"] = String.Format("{0}{1}", "Name ", i + 1);
  31.                         source.Rows.Add(dr);
  32.                     }
  33.                     Session["source"] = source;
  34.  
  35.                 }
  36.                 return source;
  37.             }
  38.             set
  39.             {
  40.                 source = value;
  41.                 Session["source"] = source;
  42.             }
  43.         }
  44.         private System.Data.DataTable source;
  45.  
  46.  
  47.         protected void Page_Load(object sender, EventArgs e)
  48.         { }
  49.  
  50.         protected void DeleteSelectedRecord_Click(object sender, EventArgs e)
  51.         {
  52.             foreach (ListItem checkBoxListItem in theCheckboxList.Items)
  53.             {
  54.  
  55.                 if (checkBoxListItem.Selected)
  56.                 {
  57.                     int itemID;//Retrieving the item's ID
  58.                     int.TryParse(checkBoxListItem.Value, out itemID);
  59.  
  60.                     //This is where you would update the database based on the record changed
  61.                     //I'm not connected to a database so I am simply updating the in memory DataTable
  62.                     string selectFilter = String.Format("{0}{1}{2}", "ID='", itemID, "'");//Used to retrieve the item checked from the source (DataTable) for the grid
  63.                     System.Data.DataRow[] rows = Source.Select(selectFilter);//The rows that match the ID selected...this should be just 1 row.
  64.                     if (rows.Length > 0)
  65.                         rows[0].Delete();//here you would go to the database and delete the record
  66.                 }
  67.             }
  68.             Source = source;//Saving the source for next time
  69.         }
  70.  
  71.         protected void ResetDataSource_Click(object sender, EventArgs e)
  72.         {
  73.             Source = null;
  74.         }
  75.  
  76.         void Page_PreRender(object sender, EventArgs e)
  77.         {
  78.             //Getting the source for the CheckBoxList and binding the CheckBoxList to the source.
  79.             theCheckboxList.DataSource = Source;
  80.             theCheckboxList.DataTextField = "Name";
  81.             theCheckboxList.DataValueField = "ID";
  82.             theCheckboxList.DataBind();
  83.         }
  84.     }
  85. }
-Frinny

1 1800
Frinavale
9,735 Expert Mod 8TB
In the Button Click event, loop through all of the list items in the CheckBoxList and delete the corresponding record if it is selected.

Here's a working example.

The ASP markup code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title>Exploring</title>
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.     <div>
  11.         <asp:CheckBoxList ID="theCheckboxList" runat="server">
  12.         </asp:CheckBoxList>
  13.         <asp:Button ID="DeleteSelectedRecord" runat="server" OnClick="DeleteSelectedRecord_Click" Text="Delete Checked Items"/>
  14.          <asp:Button ID="ResetDataSource" runat="server" OnClick="ResetDataSource_Click" Text="Reset Data Source"/>
  15.     </div>
  16.     </form>
  17. </body>
  18. </html>
The C# code behind:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class WebForm1 : System.Web.UI.Page
  11.     {
  12.         /// <summary>
  13.         /// This property is used to get and set the source for the grid.
  14.         /// If there is not already a source stored in session, it creates one.
  15.         /// </summary>
  16.         private System.Data.DataTable Source
  17.         {
  18.             get
  19.             {
  20.                 source = (System.Data.DataTable)Session["source"];
  21.                 if (source == null)
  22.                 {
  23.                     source = new System.Data.DataTable();
  24.                     source.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
  25.                     source.Columns.Add(new System.Data.DataColumn("Name"));
  26.                     for (int i = 0; i < 10; i++)
  27.                     {
  28.                         System.Data.DataRow dr = source.NewRow();
  29.                         dr["ID"] = i;
  30.                         dr["Name"] = String.Format("{0}{1}", "Name ", i + 1);
  31.                         source.Rows.Add(dr);
  32.                     }
  33.                     Session["source"] = source;
  34.  
  35.                 }
  36.                 return source;
  37.             }
  38.             set
  39.             {
  40.                 source = value;
  41.                 Session["source"] = source;
  42.             }
  43.         }
  44.         private System.Data.DataTable source;
  45.  
  46.  
  47.         protected void Page_Load(object sender, EventArgs e)
  48.         { }
  49.  
  50.         protected void DeleteSelectedRecord_Click(object sender, EventArgs e)
  51.         {
  52.             foreach (ListItem checkBoxListItem in theCheckboxList.Items)
  53.             {
  54.  
  55.                 if (checkBoxListItem.Selected)
  56.                 {
  57.                     int itemID;//Retrieving the item's ID
  58.                     int.TryParse(checkBoxListItem.Value, out itemID);
  59.  
  60.                     //This is where you would update the database based on the record changed
  61.                     //I'm not connected to a database so I am simply updating the in memory DataTable
  62.                     string selectFilter = String.Format("{0}{1}{2}", "ID='", itemID, "'");//Used to retrieve the item checked from the source (DataTable) for the grid
  63.                     System.Data.DataRow[] rows = Source.Select(selectFilter);//The rows that match the ID selected...this should be just 1 row.
  64.                     if (rows.Length > 0)
  65.                         rows[0].Delete();//here you would go to the database and delete the record
  66.                 }
  67.             }
  68.             Source = source;//Saving the source for next time
  69.         }
  70.  
  71.         protected void ResetDataSource_Click(object sender, EventArgs e)
  72.         {
  73.             Source = null;
  74.         }
  75.  
  76.         void Page_PreRender(object sender, EventArgs e)
  77.         {
  78.             //Getting the source for the CheckBoxList and binding the CheckBoxList to the source.
  79.             theCheckboxList.DataSource = Source;
  80.             theCheckboxList.DataTextField = "Name";
  81.             theCheckboxList.DataValueField = "ID";
  82.             theCheckboxList.DataBind();
  83.         }
  84.     }
  85. }
-Frinny
Mar 12 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: apple | last post by:
Hello, please help me I create a set of custom controls with IPostBackDataHandler, and postback processing. All worked correctly, only custom checkbox control postback value change only if I do...
6
by: Francois | last post by:
Hi, I am trying to implement a custom control that extends the standard System.Web.UI.WebControls.CheckBox control. In the .Net class library reference it is said that CheckBox Implements...
10
by: Jennyfer J Barco | last post by:
Hello, I have a datagrid that brings some information from a query. I need to have a checkbox in each row so the user can select the rows he wants to reprint. Is it possible to have a checkbox...
0
by: Graham | last post by:
Is there a known bug with the CheckBox control? I have subclassed the control and I am trying to add an onclick attribute in the code behind. I've tried putting the Attributes.Add in the Page_Load,...
4
by: Steven | last post by:
I am writing VB.NET, Can I add the checkbox control to a particular column on the datagrid? Thanks!
1
by: JP | last post by:
I have images within a datalist control. Below is a checkbox control that contains the SQL ID of the image blob. When the page is post back I want to iterate though checkbox collection to get...
7
by: schaefty | last post by:
I recently posted this on the incorrect forum, and so I would like to ask the question here. I did get some good ideas on the other group, but I have not yet come up with a solution for me. I am...
1
by: mani.shanku | last post by:
hi friendsplz..help me its urgent..... im making a jobportal site..when a person go for search the results should desplay.... like the naukri.com desplays..and when a person click on the checkbox...
41
bre1603
by: bre1603 | last post by:
I have a continuous form in Access 2007 called “Leadership Contact List.” It has a checkbox control for each record (bound to a field in the underlying table) that is used to email or create mailing...
1
by: Adewunmi Agbato | last post by:
I just developing my web design skills. I have a Datagrid with 2 textbox template columnsand two checkbox template columns. I have two other Textbox controls and two CheckBox controls. I have an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.