locked
Error - The name chlBoxList does not exist in the current context RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code

    protected void gvwBfbStructure_RowDataBound(object sender, GridViewRowEventArgs e)
            {
    
                using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                {
                    SqlCommand cmd = new SqlCommand("Select Location from [Loc]", con);
                    cmd.CommandType = CommandType.Text;
                        con.Open();
    
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Location"].ToString();
                            chkBoxList.Items.Add(item);
                        }
                    }
                    con.Close();
                }
                
            }

    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBoxList ID="chkBoxList" runat="server"></asp:CheckBoxList>
    </ItemTemplate>
    </asp:TemplateField>

    Thanks

    Wednesday, August 8, 2018 12:16 PM

All replies

  • User-369506445 posted

    hi

    it's <g class="gr_ gr_38 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="38" data-gr-id="38">correctm</g> you can access it, if you want to access it you have to find it in parent control, try this

    
    foreach(GridViewRow row in gvwBfbStructure.Rows) {
        if(row.RowType == DataControlRowType.DataRow) {
            CheckBoxList checkBoxList = row.FindControl("chkBoxList") as CheckBoxList ;
        }
    }

    Wednesday, August 8, 2018 12:45 PM
  • User-1171043462 posted

    This way

    protected void gvwBfbStructure_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                //Find the CheckBoxList in GridView Row
                CheckBoxList chkBoxList = (e.Row.FindControl("chkBoxList") as CheckBoxList);
                using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                {
                    SqlCommand cmd = new SqlCommand("Select Location from [Loc]", con);
                    cmd.CommandType = CommandType.Text;
                        con.Open();
    
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Location"].ToString();
                            chkBoxList.Items.Add(item);
                        }
                    }
                    con.Close();
                }
                
            }

    Wednesday, August 8, 2018 8:53 PM