473,320 Members | 1,868 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,320 software developers and data experts.

I want to maintain selected checkboxes in gridview while paging

Expand|Select|Wrap|Line Numbers
  1.  protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
  2.     {
  3.  
  4.         GridView2.PageIndex = e.NewPageIndex;
  5.         selectedCheckboxes();
  6.         BindGrid();
  7.     }
  8. private void BindGrid()
  9.     {
  10.         try
  11.         {
  12.             lblError.Visible = false;
  13.             SqlCommand cmd2 = sqlcon.CreateCommand();
  14.             cmd2.CommandText = "SELECT * FROM ImportData order by UserID";
  15.  
  16.             DataSet sds = new DataSet();
  17.  
  18.             SqlDataAdapter sda = new SqlDataAdapter(cmd2);
  19.  
  20.             sda.Fill(sds, "ImportData");
  21.             GridView2.DataSource = sds.Tables["ImportData"].DefaultView;
  22.             GridView2.DataBind();
  23.         }
  24.         catch (Exception ex)
  25.         {
  26.             lblError.Visible = true;
  27.             lblError.Text = ex.Message;
  28.         }
  29.  
  30.     }
  31.       <asp:GridView
  32.     ID="GridView2"
  33.     OnPageIndexChanging="GridView2_PageIndexChanging"
  34.     runat="server"
  35.     AutoGenerateColumns="False"
  36.     BorderStyle="Solid"
  37.     CellPadding="4"
  38.     DataKeyNames="UserID"
  39.     BorderColor="Silver"
  40.     BorderWidth="1px"
  41.     Width="300px"
  42.     AllowPaging="True"
  43.  
  44.     PageSize="4"
  45.     ShowHeader="False">
  46.  
  47. <Columns>
  48.  
  49. <asp:TemplateField>
  50.  
  51. <ItemTemplate>
  52.  
  53.  
  54.  
  55. <asp:CheckBox
  56.     ID="chk1"
  57.     runat="server"
  58.     Text='<%#DataBinder.Eval( Container.DataItem, "UserID") +"      "+DataBinder.Eval( Container.DataItem, "Name") %>' />
  59.  
  60.  
  61. </ItemTemplate>
  62.  
  63. </asp:TemplateField>
  64.  
  65. </Columns>
  66.  
  67. <HeaderStyle HorizontalAlign="Left" />
  68.  
  69.  
  70. </asp:GridView>      
  71.         <asp:HiddenField ID="hiddenCatIDs" runat="server" Visible="true"/>
  72.     </div>
  73.  
  74.  protected void selectedCheckboxes()
  75.     {
  76.  
  77.         string[] hiddenIDs = new string[] { };
  78.  
  79.         if (hiddenCatIDs.Value != string.Empty)
  80.         {
  81.             hiddenIDs = hiddenCatIDs.Value.Split(new char[] { '|' });
  82.         }
  83.  
  84.         ArrayList arrIDs = new ArrayList();
  85.  
  86.         string CatID = "0";
  87.  
  88.         if (hiddenIDs.Length != 0)
  89.         {
  90.             arrIDs.AddRange(hiddenIDs);
  91.         }
  92.  
  93.         CheckBox chk;
  94.  
  95.         foreach (GridViewRow rowItem in GridView2.Rows)
  96.         {
  97.  
  98.             chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1"));
  99.             CatID = GridView2.DataKeys[rowItem.RowIndex]["UserID"].ToString();
  100.  
  101.             if (chk.Checked)
  102.             {
  103.                 Response.Write("chk.Checked in if :" + chk.Checked + "<br />");
  104.                 if (!arrIDs.Contains(CatID))
  105.                 {
  106.                     arrIDs.Add(CatID);
  107.                 }
  108.             }
  109.             else
  110.             {
  111.                 Response.Write("chk.Checked in else :" + chk.Checked + "<br />");
  112.                 if (arrIDs.Contains(CatID))
  113.                 {
  114.                     arrIDs.Remove(CatID);
  115.                 }
  116.             }
  117.         }
  118.  
  119.         // ArrayList collection converted into string array.
  120.         hiddenIDs = (string[])arrIDs.ToArray(typeof(String));
  121.  
  122.  
  123.         // Join function of string array is used here to create a '|' separated string that can be stored in the hiddenCatIDs control.
  124.         // HiddenField Control is used here to maintain the checkbox state while GridView paging.
  125.         hiddenCatIDs.Value = string.Join("|", hiddenIDs);
  126.  
  127.         Response.Write(hiddenCatIDs.Value);
  128.     }
  129.     protected void btnMultipleRowDelete_Click(object sender, EventArgs e)
  130.     {
  131.         // Looping through all the rows in the GridView
  132.         foreach (GridViewRow row in GridView1.Rows)
  133.         {
  134.             CheckBox checkbox = (CheckBox)row.FindControl("cbRows");
  135.  
  136.             //Check if the checkbox is checked.
  137.             //value in the HtmlInputCheckBox's Value property is set as the //value of the delete command's parameter.
  138.             if (checkbox.Checked)
  139.             {
  140.                 // Retreive the User ID
  141.                 int UserID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
  142.                 // Pass the value of the selected User  ID to the Delete //command.
  143.                 SqlDataSource1.DeleteParameters["UserID"].DefaultValue = UserID.ToString();
  144.  
  145.                 SqlDataSource1.Delete();
  146.             }
  147.         }
  148.     }
  149.  
Aug 5 '10 #1
0 1542

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

Similar topics

1
by: zoneal | last post by:
Hello there, Currently I am working with the project which needs dynamically generated checkboxes and i have generated dynamic checkboxes but i am facing problem with getting the value of...
0
by: wei | last post by:
Hello, all I get a little problem about this gridview paging event, it's work prefect in my local machine, even I used my IP address instead "localhost". when people use this web page, and try to...
2
by: Bishop | last post by:
The default gridview paging links are JavaScript and it looks like there is no way for search engines to follow them. Any ideas on what I can do to allow search engines to follow the links so they...
0
by: =?Utf-8?B?V0I=?= | last post by:
Hi, I have a .NET page which has a gridview of customers from Northwind database. There's a checkbox for each customer and the gridview allows paging. I would like to be able to persist the...
0
by: jlrolin | last post by:
Tearing my hair out over this, and it may be a postback problem of some sort. I have everything displaying, etc. BUT when I click the "Run Process" button, I want to grab the selected checkboxes...
0
by: Don Miller | last post by:
Without a SQLDataSource declaratively linked to a GridView, I programmatically create a dataset and bind it to a GridView with EnableSortingAndPagingCallbacks=True and Paging=True. The GridView...
0
by: Don Miller | last post by:
Here is an example of what I believe is a bug in ASP.NET 2.0 GridView paging without postbacks (or at least not documented how to fix it). Once the GridView is displayed, clicking on any of the...
1
by: Matt Winward | last post by:
Hi all. I've got a little problem with my gridview paging and I'm not sure how to resolve it. Basically, I'm handling all aspx requests with my own httphandler. This does a url rewrite, so...
2
by: Ananthu | last post by:
Hi, I have a gridview with paging option. I want to maintain the state of the checkbox column in gridview for each page. I used vb.net coding. I used the following link for this purpose, ...
3
by: vinpkl | last post by:
hi all i want to insert all selected checkboxes values with a comma separator in database in one single column. i m able to echo all the checkboxes selected through the below code <input...
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...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.