datagrid with checkboxes

M

Mortar

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.FindControl("chkCol");
if ((cb != null) && cb.Checked)
{
a += 1;
// need to get ID from 1st datagrid column for this checkbox

}
}

sqlBuild();
}
 
D

David Jessee

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.
 
M

Mortar

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?


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 said:
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.FindControl("chkCol");
if ((cb != null) && cb.Checked)
{
a += 1;
// need to get ID from 1st datagrid column for this checkbox

}
}

sqlBuild();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top