Avatar of Brian
Brian
Flag for United States of America asked on

How to display Check in CheckBox Control

Hello EE,

I have the following CodeBehind below and I would like to display a Check in my CheckBox Control during Page_Load if the value for the CheckBox Control is 1 in the DB.

 protected void Page_Load(object sender, EventArgs e)
    {
        hf_AppID.Value = Session["pi_id"].ToString();

        if (!IsPostBack)
        {
            int pi_id = Convert.ToInt32(Session["pi_id"]);

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "WellnessChoice_RetrieveAnnualPhysicalValuesByPI_ID";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = pi_id;

            DataTable dtModify = new DataTable("Modify");

            SqlDataAdapter adp = new SqlDataAdapter();

            try
            {
                conn.Open();

                adp.SelectCommand = cmd;
                adp.Fill(dtModify);

                if ((dtModify != null))
                {
                    DataRow data = dtModify.Rows[0];
                    hf_ap_id.Value = data["ap_id"].ToString();
                    txtPhysicalDateCompleted.Text = Convert.ToDateTime(data["ap_date"]).ToShortDateString();
                    lblFileNameUploaded.Text = "Your File has been uploaded: " + data["ap_pdf_filename"].ToString();

                    if (data["ap_section_complete"].ToString() == "1")
                    {
                        btn_SaveAnnualPhysical.Enabled = false;
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
ASP.NET.NET Programming

Avatar of undefined
Last Comment
Brian

8/22/2022 - Mon
disrupt

To check a checkbox use:

Checkbox1.checked = true;

what exactly are you stuck on?
Brian

ASKER
Hi discrupt,

Below is my code. What i'm trying to accomplish is to disable the Button Control to prevent users from submitting more data if the ChecBox Control labeled (cb_AnnualPhysical) is equal to "1" in the DB and if so disable Button Control labeled (btn_SaveAnnualPhysical) and place Check in CheckBox Control. This all needed to happen in Page_Load. So far it works as I needed but perhaps you can look it over and give me your feedback.

    protected void Page_Load(object sender, EventArgs e)
    {
        hf_AppID.Value = Session["pi_id"].ToString();

        if (!IsPostBack)
        {
            int pi_id = Convert.ToInt32(Session["pi_id"]);

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "WellnessChoice_RetrieveAnnualPhysicalValuesByPI_ID";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = pi_id;

            DataTable dtModify = new DataTable("Modify");

            SqlDataAdapter adp = new SqlDataAdapter();

            try
            {
                conn.Open();

                adp.SelectCommand = cmd;
                adp.Fill(dtModify);

                if ((dtModify != null))
                {
                    DataRow data = dtModify.Rows[0];
                    hf_ap_id.Value = data["ap_id"].ToString();
                    txtPhysicalDateCompleted.Text = Convert.ToDateTime(data["ap_date"]).ToShortDateString();
                    lblFileNameUploaded.Text = "Your File has been uploaded: " + data["ap_pdf_filename"].ToString();

                    if (data["ap_section_complete"].ToString() == "1")
                    {
                        btn_SaveAnnualPhysical.Enabled = false;
                        cb_AnnualPhysical.Checked = true;
                    }

                    else
                    {
                        cb_AnnualPhysical.Checked = false;
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

protected void Page_Load(object sender, EventArgs e)
{
.. code removed for brevity
}
disrupt

Looks good to me!
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
disrupt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Brian

ASKER
Ok, Thank you for looking it over!