473,473 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

datagrid with checkboxes

i have a datagrid with 2 columns.

the 1st column contains an id which will be used by the database for
the selected checkbox records.
the 2nd column is a template column containing a server checkbox
control.

where i'm at:
i can retrieve a count of the selected checkboxes, but don't know how
to get the related id's (in the 1st column).

what i would like to know:
- obviously, how to retrieve the id's for the checkboxes
- on a postback, why do i have to re-create the datagrid on the server
before i can get the selected checkboxes? in other words, if i have a
server function which loops through the datagrid's selected
checkboxes, it won't work unless i create and populate the datagrid
again 1st. It seems inefficient for the server to :
1) re-create the datagrid,
2) then grab the selected checkboxes,
3) do database stuff,
4) then re-create the datagrid again before sending to the client.

why can't i just do steps 2,3 and 4?

my code follows. If someone could fill in my for loop to grab the id's
from the 1st column in the datagrid, i would be grateful.

-----------
<asp:datagrid id="dgResults" runat="server"AutoGenerateColumns="False"
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"
Visible="false">
<ItemStyle Width="0px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemStyle Width="30px"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkCol" Runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
-----------
-----------
private void imgSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
int a = 0;
DataGridItemCollection items=dgResults.Items;
for (int i=0; i<items.Count; i++)
{
CheckBox cb= (CheckBox)items[i].FindControl("chkCol");
if ((cb != null) && cb.Checked)
{
a += 1;
// need to get ID from 1st datagrid column for this checkbox

}
}

sqlBuild();
}
-----------

if i'm doin this all wrong, suggestions (with code) would be pleasant.
Nov 19 '05 #1
2 2435
if you allow your datagrid to maintain its viewstate, then you don't have to
re-poll the database.

to get the Id's, set your grid's DataKeyField to "Id" or whateer you need.
then you can refer to DateKeys[2] or DataKeys[3] to get the key for the
appropriate row index.

"Mortar" <a@b.com> wrote in message
news:41***************@nntp.broadband.rogers.com.. .
i have a datagrid with 2 columns.

the 1st column contains an id which will be used by the database for
the selected checkbox records.
the 2nd column is a template column containing a server checkbox
control.

where i'm at:
i can retrieve a count of the selected checkboxes, but don't know how
to get the related id's (in the 1st column).

what i would like to know:
- obviously, how to retrieve the id's for the checkboxes
- on a postback, why do i have to re-create the datagrid on the server
before i can get the selected checkboxes? in other words, if i have a
server function which loops through the datagrid's selected
checkboxes, it won't work unless i create and populate the datagrid
again 1st. It seems inefficient for the server to :
1) re-create the datagrid,
2) then grab the selected checkboxes,
3) do database stuff,
4) then re-create the datagrid again before sending to the client.

why can't i just do steps 2,3 and 4?

my code follows. If someone could fill in my for loop to grab the id's
from the 1st column in the datagrid, i would be grateful.

-----------
<asp:datagrid id="dgResults" runat="server"AutoGenerateColumns="False"
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"
Visible="false">
<ItemStyle Width="0px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemStyle Width="30px"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkCol" Runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
-----------
-----------
private void imgSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
int a = 0;
DataGridItemCollection items=dgResults.Items;
for (int i=0; i<items.Count; i++)
{
CheckBox cb= (CheckBox)items[i].FindControl("chkCol");
if ((cb != null) && cb.Checked)
{
a += 1;
// need to get ID from 1st datagrid column for this checkbox

}
}

sqlBuild();
}
-----------

if i'm doin this all wrong, suggestions (with code) would be pleasant.

Nov 19 '05 #2

with several hundred rows of data, 20 columns wide, with
enableviewstate on, performance really takes a hit. So either I have
to do that, OR re-create the grid, OR store the keys in a session
variable. Or the last method which i think i like the best is to use
client side script to loop through the checked boxes and store their
name (which includes ID value) into a hidden text field. Then pass
this hidden value to the server. That way, no extra stuff being done
on the server. What ya think?
On Tue, 25 Jan 2005 01:09:17 -0600, "David Jessee"
<dj*****@houston.rr.com> wrote:
if you allow your datagrid to maintain its viewstate, then you don't have to
re-poll the database.

to get the Id's, set your grid's DataKeyField to "Id" or whateer you need.
then you can refer to DateKeys[2] or DataKeys[3] to get the key for the
appropriate row index.

"Mortar" <a@b.com> wrote in message
news:41***************@nntp.broadband.rogers.com. ..
i have a datagrid with 2 columns.

the 1st column contains an id which will be used by the database for
the selected checkbox records.
the 2nd column is a template column containing a server checkbox
control.

where i'm at:
i can retrieve a count of the selected checkboxes, but don't know how
to get the related id's (in the 1st column).

what i would like to know:
- obviously, how to retrieve the id's for the checkboxes
- on a postback, why do i have to re-create the datagrid on the server
before i can get the selected checkboxes? in other words, if i have a
server function which loops through the datagrid's selected
checkboxes, it won't work unless i create and populate the datagrid
again 1st. It seems inefficient for the server to :
1) re-create the datagrid,
2) then grab the selected checkboxes,
3) do database stuff,
4) then re-create the datagrid again before sending to the client.

why can't i just do steps 2,3 and 4?

my code follows. If someone could fill in my for loop to grab the id's
from the 1st column in the datagrid, i would be grateful.

-----------
<asp:datagrid id="dgResults" runat="server"AutoGenerateColumns="False"
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"
Visible="false">
<ItemStyle Width="0px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemStyle Width="30px"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkCol" Runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
-----------
-----------
private void imgSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
int a = 0;
DataGridItemCollection items=dgResults.Items;
for (int i=0; i<items.Count; i++)
{
CheckBox cb= (CheckBox)items[i].FindControl("chkCol");
if ((cb != null) && cb.Checked)
{
a += 1;
// need to get ID from 1st datagrid column for this checkbox

}
}

sqlBuild();
}
-----------

if i'm doin this all wrong, suggestions (with code) would be pleasant.



Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: john | last post by:
I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the user clicks on a certain button on the page, which is not in the grid, I want to be able to...
2
by: Mortar | last post by:
i have a datagrid with a column of html checkboxes which are created dynamically. The id/name of the checkboxes comes from a value in the database. on a postback, i would like to get all the...
1
by: kannadasan | last post by:
Hi all I am using Datagrid where i place checkboxes in one column with some other columns.The purpose is, if i select the checkboxes and clicks the submit buton Email has to go to the selected...
7
by: DJ Dev | last post by:
Hi All, I have a complex problem. I have dropdownlists (usually 3-5) and the user selects some value from these and for each value selected, datagrids are shown to the user. I am creating the...
2
by: john | last post by:
I posted this question to comp.lang.javascript but didn't get a response, so I'll try here. I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the...
7
by: Jaime Stuardo | last post by:
Hi all.. I have a DataGrid with checkboxes. In the header I have a "check all" checkbox. I'm wondering if there is an easy way to check all checkboxes using that checkbox. I could do it using...
3
by: GatorBait | last post by:
Hi all, I'm using a datagrid for the first time and I am running into some problems that I hope someone can help me with. I have a datagrid with 18 rows and 5 columns....column 1 is just text...
7
by: rn5a | last post by:
The first column of a DataGrid has a CheckBox for all the rows. I want that when users check a CheckBox, the BackColor of that entire row in the DataGrid should change to a different color. To...
5
by: rn5a | last post by:
In my application, I want to populate all the directories & files existing in a directory on the server in a DataGrid. To ensure that all the directories get listed first followed by all the files,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.