need to get unique ids of selected checkboxes in datagrid

N

nasirmajor

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

Bruno Alexandre

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
 
N

nasirmajor

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.FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

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

}
a += 1;
}
 
B

Bruno Alexandre

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

Bruno Alexandre

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

nasirmajor

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.FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

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

}
a += 1;
}
 
N

nasirmajor

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.FindControl("cb");
if ((cb2 != null) && cb2.Checked)
{

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

}
a += 1;
}
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top