Set CheckBoxList Item checked (selected) on GridView Edit Button Click using C# in ASP.Net

Last Reply 21 days ago By itsme

Posted 21 days ago

I am trying  to Set the checked value on edit button click  by user so that user can edit and update there record. But I dont know how to do so.

I know how to do this in dropdownlist as you can see in my code but i dont know how to do this in checkbox

Thanks

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem>Card</asp:ListItem>
    <asp:ListItem>Cash</asp:ListItem>
</asp:CheckBoxList>

 

        protected void btnEdit_Click(object sender, EventArgs e)
        {            
            try
            {
                Button btn = sender as Button;
                GridViewRow grow = btn.NamingContainer as GridViewRow;
                Id.Value = (grow.FindControl("Id") as Label).Text;
                Image img = (grow.FindControl("img") as Image);
                txt_email.Text = (grow.FindControl("lblemail") as Label).Text;
                txt_link.Text = (grow.FindControl("lblweb") as Label).Text;
                txt_img.Text = (grow.FindControl("lblimg") as Label).Text;
                txt_name.Text = (grow.FindControl("lblname") as Label).Text;
                txt_pay.Text = (grow.FindControl("lblpay") as Label).Text;
                txt_toll.Text = (grow.FindControl("lbltoll") as Label).Text;
                txt_Address.Text = (grow.FindControl("lblAddress") as Label).Text;
                Image1.ImageUrl = img.ImageUrl;
                btnUpdate.Visible = true;
                string value = (grow.FindControl("lblBname") as Label).Text;
                DropDownList1.ClearSelection();
                DropDownList1.Items.FindByText(value).Selected = true;
                string value2 = (grow.FindControl("lblcity") as Label).Text;
                DropDownList2.ClearSelection();
                DropDownList2.Items.FindByValue(value2).Selected = true;
                
                string from = (grow.FindControl("lblopen") as Label).Text;
                string[] parts = from.Split(',');
                ddlTimeFrom.ClearSelection();ddlTimeTo.ClearSelection();
                ddlTimeFrom.Items.FindByValue(parts[0].Trim()).Selected = true;
                ddlTimeTo.Items.FindByValue(parts[1].Trim()).Selected = true;  
            }
            catch(Exception ex)
            {
                Response.Write(ex);
            }
        }       

 

You are viewing reply posted by: itsme 21 days ago.
Posted 21 days ago

I found the answer and it is working

Thanks

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem>Card</asp:ListItem>
    <asp:ListItem>Cash</asp:ListItem>
</asp:CheckBoxList>

 

        private void UpdateFile(string path, string fileName)
        {
            List<String> open = new List<String>();
            if (ddlTimeFrom.SelectedIndex > 0 && ddlTimeTo.SelectedIndex > 0)
            {
                open.Add(ddlTimeFrom.Text);
                open.Add(ddlTimeTo.Text);
            }
            string open2 = string.Join(", ", open);
            string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("UPDATE SP_Afterlogin2 SET Images =@Path,Bussiness_name=@Bussiness_name,city=@city,Image_name=@Image_name,name=@name,toll_free=@toll_free,website_link=@website_link,payment_mode=@payment_mode,email=@email,address=@address,h_open=@h_open WHERE Id =@Id", con);
            cmd.Parameters.AddWithValue("@Id", Id.Value);
            cmd.Parameters.AddWithValue("@Path", path);
            cmd.Parameters.AddWithValue("@Bussiness_name", DropDownList1.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@city", DropDownList2.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@name", txt_name.Text);
            cmd.Parameters.AddWithValue("@toll_free", txt_toll.Text);
            cmd.Parameters.AddWithValue("@website_link", txt_link.Text);
            cmd.Parameters.AddWithValue("@payment_mode",CheckBoxList1.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@email", txt_email.Text);
            cmd.Parameters.AddWithValue("@Image_name",fileName);
            cmd.Parameters.AddWithValue("@address", txt_Address.Text);
            cmd.Parameters.AddWithValue("@h_open", open2);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();         
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
                ClearControls(); 
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {            
            try
            {
                Button btn = sender as Button;
                GridViewRow grow = btn.NamingContainer as GridViewRow;
                Id.Value = (grow.FindControl("Id") as Label).Text;
                Image img = (grow.FindControl("img") as Image);
                txt_email.Text = (grow.FindControl("lblemail") as Label).Text;
                txt_link.Text = (grow.FindControl("lblweb") as Label).Text;
                txt_img.Text = (grow.FindControl("lblimg") as Label).Text;
                txt_name.Text = (grow.FindControl("lblname") as Label).Text;
                //CheckBoxList1.Text = (grow.FindControl("lblpay") as Label).Text;
                
                txt_toll.Text = (grow.FindControl("lbltoll") as Label).Text;
                txt_Address.Text = (grow.FindControl("lblAddress") as Label).Text;
                Image1.ImageUrl = img.ImageUrl;
                btnUpdate.Visible = true;
                string value = (grow.FindControl("lblBname") as Label).Text;
                DropDownList1.ClearSelection();
                DropDownList1.Items.FindByText(value).Selected = true;
                string value2 = (grow.FindControl("lblcity") as Label).Text;
                DropDownList2.ClearSelection();
                DropDownList2.Items.FindByValue(value2).Selected = true;
                
                string from = (grow.FindControl("lblopen") as Label).Text;
                string[] parts = from.Split(',');
                ddlTimeFrom.ClearSelection();ddlTimeTo.ClearSelection();
                ddlTimeFrom.Items.FindByValue(parts[0].Trim()).Selected = true;
                ddlTimeTo.Items.FindByValue(parts[1].Trim()).Selected = true;

                string chk = (grow.FindControl("lblpay") as Label).Text;
                string[] sp = chk.Split(',');
                CheckBoxList1.ClearSelection();
                CheckBoxList1.Items.FindByValue(sp[0].Trim()).Selected = true;
                CheckBoxList1.Items.FindByValue(sp[1].Trim()).Selected = true;
            }
            catch(Exception ex)
            {
                Response.Write(ex);
            }
        }