How to display Check in CheckBox Control

asp_net2
asp_net2 used Ask the Experts™
on
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();
            }
        }
    }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
To check a checkbox use:

Checkbox1.checked = true;

what exactly are you stuck on?

Author

Commented:
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
}

Commented:
Looks good to me!
Commented:
you may want to add:

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

                    else
                    {
                        btn_SaveAnnualPhysical.Enabled = true;
                        cb_AnnualPhysical.Checked = false;
                    }

depending if you are going to toggle it

Author

Commented:
Ok, Thank you for looking it over!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial