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

need to get unique ids of selected checkboxes in datagrid

P: n/a
dear all,
i have a datagrid with checkboxes for selection of rows. and want to
get unique id/ids(job_code) from the selected rows in array etc.
can anyone help.
<asp:datagrid id="dgResults" runat="server" DataKeyField="job_code">
<Columns>
<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 IDs from datagrid for the rows which are
selected with checkbox
}
}
}

thanks in advance

Jun 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
first off all, you forgot to paste the rest of the code, cause you are
calling for imgSubmit_Click and inside the DataGrid there's no imgSubmit
object :-/
add to the image tag
CommandName='<%# Eval("job_code") %>'
CommandArgument='<%# Eval("job_code") %>'
OnCommand="imgSubmit_Command"

and add the Protected Sub img_Command(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs)
inside you can use e.CommandArgument to get the job_code for that line. And
do the procedure in onCommand instead onClick
--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)
<na********@yahoo.com> escreveu na mensagem
news:11*********************@c74g2000cwc.googlegro ups.com...
dear all,
i have a datagrid with checkboxes for selection of rows. and want to
get unique id/ids(job_code) from the selected rows in array etc.
can anyone help.
<asp:datagrid id="dgResults" runat="server" DataKeyField="job_code">
<Columns>
<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 IDs from datagrid for the rows which are
selected with checkbox
}
}
}

thanks in advance

Jun 21 '06 #2

P: n/a
Dear Bruno,
there is no image tag its simply a button, while copy pasting i did'nt
changed its name
you can re see my code and can help me again thank you again in
advance.

protected void btnapplyto_Click(object sender, EventArgs e)
{
int a = 0;
DataGridItemCollection items = l.Items;
for (int i = 0; i < items.Count; i++)
{
CheckBox cb2 = (CheckBox)items[i].FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

// need to get ID from 1st datagrid column for this
checkbox

}
a += 1;
}

Jun 21 '06 #3

P: n/a
so you have 2 Datagrids!!!

you never mention that before :-)
because you are not firing any event in the DG that has the checkboxes, you
need to run all of them and what you can do is have the VALUE of the
checkbox bind to the ID of the ROW...

so, whenever you get a checked Checkbox you just see it's value and you have
the row ID

because Checkboxes don't have Value property you can convert it to a
CheckboxList with 1 ListItem, and that you have the value property that you
can use.

--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)
<na********@yahoo.com> escreveu na mensagem
news:11*********************@r2g2000cwb.googlegrou ps.com...
Dear Bruno,
there is no image tag its simply a button, while copy pasting i did'nt
changed its name
you can re see my code and can help me again thank you again in
advance.

protected void btnapplyto_Click(object sender, EventArgs e)
{
int a = 0;
DataGridItemCollection items = l.Items;
for (int i = 0; i < items.Count; i++)
{
CheckBox cb2 = (CheckBox)items[i].FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

// need to get ID from 1st datagrid column for this
checkbox

}
a += 1;
}

Jun 21 '06 #4

P: n/a
do you really need the checkboxes be in a different DG? can't you just have
1 CheckboxList with all of them? you can provide a DataSource to a CBL...

--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)
<na********@yahoo.com> escreveu na mensagem
news:11*********************@r2g2000cwb.googlegrou ps.com...
Dear Bruno,
there is no image tag its simply a button, while copy pasting i did'nt
changed its name
you can re see my code and can help me again thank you again in
advance.

protected void btnapplyto_Click(object sender, EventArgs e)
{
int a = 0;
DataGridItemCollection items = l.Items;
for (int i = 0; i < items.Count; i++)
{
CheckBox cb2 = (CheckBox)items[i].FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

// need to get ID from 1st datagrid column for this
checkbox

}
a += 1;
}

Jun 21 '06 #5

P: n/a
Dear Bruno,
again disturbing
there is only one datagrid
protected void btnapplyto_Click(object sender, EventArgs e)
{
int a = 0;
DataGridItemCollection items = l.Items;
for (int i = 0; i < items.Count; i++)
{
CheckBox cb2 = (CheckBox)items[i].FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

// need to get ID from datagrid column for this
checkbox

}
a += 1;
}


Jun 21 '06 #6

P: n/a
Dear Bruno,
again disturbing
there is only one datagrid
protected void btnapplyto_Click(object sender, EventArgs e)
{
int a = 0;
DataGridItemCollection items = l.Items;
for (int i = 0; i < items.Count; i++)
{
CheckBox cb2 = (CheckBox)items[i].FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

// need to get ID from datagrid column for this
checkbox

}
a += 1;
}


Jun 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.