Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag 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();
            }
        }
    }
Avatar of disrupt
disrupt
Flag of United States of America image

To check a checkbox use:

Checkbox1.checked = true;

what exactly are you stuck on?
Avatar of 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
}
Looks good to me!
ASKER CERTIFIED SOLUTION
Avatar of disrupt
disrupt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Ok, Thank you for looking it over!