a checkboxlist inside a gridview

Discussion in 'ASP .Net Web Controls' started by Trapulo, Apr 10, 2007.

  1. Trapulo

    Trapulo Guest

    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
     
    Trapulo, Apr 10, 2007
    #1
    1. Advertising

  2. 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.
     
    Walter Wang [MSFT], Apr 11, 2007
    #2
    1. Advertising

  3. Trapulo

    Trapulo Guest

    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.
    >
    >
     
    Trapulo, Apr 12, 2007
    #3
  4. 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.
     
    Walter Wang [MSFT], Apr 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JD
    Replies:
    5
    Views:
    11,781
    NetArchitect
    Aug 8, 2007
  2. =?Utf-8?B?dmJNZW50YWw=?=

    CheckboxList inside a usercontrol inside of a datalist.

    =?Utf-8?B?dmJNZW50YWw=?=, Oct 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    590
    =?Utf-8?B?dmJNZW50YWw=?=
    Oct 30, 2004
  3. Patrick.O.Ige
    Replies:
    5
    Views:
    19,238
    Patrick.O.Ige
    Jun 19, 2005
  4. dealkk
    Replies:
    0
    Views:
    481
    dealkk
    Jul 26, 2005
  5. HockeyFan
    Replies:
    1
    Views:
    1,018
    Masudur
    Jul 4, 2007
Loading...

Share This Page