Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > a checkboxlist inside a gridview

Reply
Thread Tools

a checkboxlist inside a gridview

 
 
Trapulo
Guest
Posts: n/a
 
      04-10-2007
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




 
Reply With Quote
 
 
 
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      04-11-2007
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 (, 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.

 
Reply With Quote
 
 
 
 
Trapulo
Guest
Posts: n/a
 
      04-12-2007
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

"Walter Wang [MSFT]" wrote:

> 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 (, 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.
>
>

 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      04-13-2007
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 (, 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkboxlist inside Repeater - doesn't fire click event for Checkboxlist JD ASP .Net 5 08-08-2007 11:28 AM
RowCommand: accessing ObjectDataSource from inside gridview inside a repeater? HockeyFan ASP .Net 1 07-04-2007 05:49 AM
how to do checkboxlist inside datalist by pull data in database. dealkk ASP .Net 0 07-26-2005 03:50 AM
How to get Selected item in a Databinded CheckBoxList when CheckBoxlist is in a DataList? Patrick.O.Ige ASP .Net 5 06-19-2005 06:11 AM
CheckboxList inside a usercontrol inside of a datalist. =?Utf-8?B?dmJNZW50YWw=?= ASP .Net 0 10-30-2004 03:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57