By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

datagrid with checkboxes

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.