a checkboxlist inside a gridview

T

Trapulo

Hello,
I need to use a checkBoxList as control in an editTemplate of a gridview. I
can insert it, and bind data. I have a lot of trouble handling selection of
data and updating it.

Basically, the gridview manages some records binded to an objectDataSource,
and I need to update/insert data. I have some standard columns (strings,
numeric, etc.) , and a lot of "bit" columns, so I want to render them as
options in a single checkesboxlist instead of a lot of different checkbox
columns.
This is my template:
<EditItemTemplate>

<asp:CheckBoxList ID="clbServices" runat="server" EnableViewState="false">

</asp:CheckBoxList>

</EditItemTemplate>

Then I bind data from gridview's rowDataBound event, calling
checkedboxList.Items.Add and setting the "Selected" property, and all works.
I cannot retrieve selected data to set parameters for the update.

Any suggestion to have this goal?



thanks
 
W

Walter Wang [MSFT]

Hi Trapulo,

You should set EnableViewState="true" for the CheckBoxList in your
EditItemTemplate otherwise the change made by user will not restored
correctly at server-side while editing the row.

To get the updated values, you need to handle GridView's RowUpdating event:

protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
CheckBoxList checklist1 =
GridView1.Rows[e.RowIndex].Cells[1].FindControl("checklist1") as
CheckBoxList;
e.NewValues["Archived"] = checklist1.Items[0].Selected;
}


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Trapulo

Thank you, my problem was related to FindControls method: I was calling it
direct on gridview object and it didn't find the checkboxlist. Calling as you
suggest works.

Thank you
 
W

Walter Wang [MSFT]

Hi,

Yes the FindControl only find a direct child in the parent container, since
the GridViewRow implements a marker interface INamingContainer, it will act
as a naming container and you must use its FindControl to find the
CheckBoxList.

As a side note, since the TableCell doesn't implement INamingContainer, we
actually don't need to use .Cells[1].FindControl:
GridView1.Rows[e.RowIndex].FindControl("checklist1") will be fine.

If you can guarantee the control ID is unique in all the descendent
controls in the gridview, you can also use following wrapper function to
find the control recursively:

public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}


CheckBoxList checklist1 = FindControlRecursive(GridView1, "checklist1") as
CheckBoxList;


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top