CheckBox in EditItemTemplate of GridView

Last post 07-26-2007 1:39 AM by Allen Chen – MSFT. 1 replies.

Sort Posts:

  • CheckBox in EditItemTemplate of GridView

    07-24-2007, 1:57 PM
    • Loading...
    • isheahan
    • Joined on 05-11-2005, 4:58 AM
    • Posts 125

    My database houses a char(7) field that contains strings in the following format:

    MTWTFSS or ------- or any combination thereof M-W-FSS or -----SS, etc...

    In the footer row of the GridView, I'm using 7 CheckBoxes and processing the bool value of each checkbox to create a string to pass to the said field. It works great and looks something similar to this:

    1        protected void gvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    2        {
    3            // if (e.CommandName == "Update")     <-- Update CommandName commented out for space's sake
    4            // if (e.CommandName == "Delete")      <-- Delete CommandName commented out for space's sake
    5            if (e.CommandName == "Save")
    6            {
    7                // Create the DOW string to upload.
    8                string strDOW;
    9                if (((CheckBox)gvGroup.FooterRow.FindControl("chkMonday")).Checked == true)
    10               {
    11                   strDOW = "M";
    12               }
    13               else
    14               {
    15                   strDOW = "-";
    16               }
    17               if (((CheckBox)gvGroup.FooterRow.FindControl("chkTuesday")).Checked == true)
    18               {
    19                   strDOW += "T";
    20               }
    21               else
    22               {
    23                   strDOW += "-";
    24               }     
    Etc...
    I need to allow the user the ability to edit the rows and give them the same checkboxes (for consistency sake) that they used to insert the row. 
    So, now I need to parse the string and pass the values accordingly back to CheckBoxes in the EditItemTemplate field. I do this by placing the
    string into a hidden Label control then reading it. So far, this is what I have:
    1            //Is Monday selected?
    2            if (((Label)gvGroup.Rows[0].FindControl("lblDOW")).Text.Substring(0, 1) == "M")
    3            {
    4                ((CheckBox)gvGroup.Rows[0].FindControl("chkMonday")).Checked = true;
    5            }
    6    
    7            //Is Tuesday selected?
    8            if (((Label)gvGroup.Rows[0].FindControl("lblDOW")).Text.Substring(1, 1) == "T")
    9            {
    10               ((CheckBox)gvGroup.Rows[0].FindControl("chkTuesday")).Checked = true;
    11           }
    
    The problem is that I get an error saying that the object is not referenced to an instance of an object with reference to the CheckBox control. 
    Say like the string looked like this: -TWT-SS, this means that Monday would be false, the substring function reads properly through this, passes
    through the conditional "if" statement and continues to evaluate the second position of the string (1,1) to find a "T" in it's place. Now it has to find
    the CheckBox and "Check" it because it's true. That's when it fails.
    Here's the aspx code:
    1    <td class="gvGroupTemplateStyle" style="width: 100px;">
    2        <asp:CheckBox ID="chkMonday" runat="server" Text="" CssClass="aspCheckBoxDOW" /><asp:CheckBox
    3            ID="chkTuesday" runat="server" Text="" CssClass="aspCheckBoxDOW" /><asp:CheckBox
    4                ID="chkWednesday" runat="server" Text="" CssClass="aspCheckBoxDOW" /><asp:CheckBox
    5                    ID="chkThursday" runat="server" Text="" CssClass="aspCheckBoxDOW" /><asp:CheckBox
    6                        ID="chkFriday" runat="server" Text="" CssClass="aspCheckBoxDOW" /><asp:CheckBox ID="chkSaturday"
    7                            runat="server" Text="" CssClass="aspCheckBoxDOW" /><asp:CheckBox ID="chkSunday" runat="server"
    8                                Text="" CssClass="aspCheckBoxDOW" /></td>
    
    Any ideas why I can't see the CheckBoxes in the EditItemTemplate and can't set their Checked attributes?
    thanks,
    isheahan
  • Re: CheckBox in EditItemTemplate of GridView

    07-26-2007, 1:39 AM
    Answer

    Hi:

      I noticed you're using gvGroup.Rows[0].FindControl("lblDOW"). This means you're trying to find the control on the first row. Please double check it and you can simply test it by setting breakpoints.

      If you still have this problem, please post your whole inline code and tell us where you put the code.

    Thanks & Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter